Print Oleobject ("word.application") with Delphi

Asked

Viewed 1,270 times

6

I’m trying to print an interval of pages from objeto OleObject ("word.application") in the delphi 6 and I’m not getting it.

Using the printout(false) = I can successfully print, but I want to print a range of pages.

Thank you so much for your help.

Code:


if (OpenDialog1.Execute) then
  begin
    try
      // Cria objeto principal de controle do Word
      WinWord := CreateOleObject('Word.Application');
      if (not (VarIsEmpty(WinWord))) then
      begin
        // Mostra o Word
        try
          WinWord.Visible := false;
          Docs := WinWord.Documents;
          // Abre um documento
          Doc := Docs.Open(OpenDialog1.FileName);

          //Doc.PrintOut(false);
          //Doc.PrintOut(Copies := 2);
          Doc.PrintOut(Background := false, Append := false, Range := wdPrintFromTo, OutputFileName := EmptyParam, From := 1, To := 2);
          // erro apresentado: "tipo não correspondente"
        finally
          // Fecha o Word
          //WinWord.close;
          //Doc.Close(SaveChanges := 0);
          WinWord.ActiveDocument.Close(SaveChanges := 0);
          WinWord.Quit;

          WinWord := Unassigned;
          Docs := Unassigned;
          Doc := Unassigned;

        end;
      end;
      //showmessage('Fim!');
    finally
    end;
  end;

1 answer

3


The method PrintOut has the following parameters:

Var Background: OleVariant;
Var Append: OleVariant; 
Var Range: OleVariant;
Var OutputFileName: OleVariant; 
Var From: OleVariant; 
Var To_: OleVariant;
Var Item: OleVariant; 
Var Copies: OleVariant; 
Var Pages: OleVariant; 
Var PageType: OleVariant; 
Var PrintToFile: OleVariant; 
Var Collate: OleVariant;
Var ActivePrinterMacGX: OleVariant; 
Var ManualDuplexPrint: OleVariant; 
Var PrintZoomColumn: OleVariant; 
Var PrintZoomRow: OleVariant; 
Var PrintZoomPaperWidth: OleVariant; 
Var PrintZoomPaperHeight: OleVariant

the property Range determines how the selection of pages will work according to the following values:

  • 0 - Print All Document
  • 1 - print selected text
  • 2 - Print the current page
  • 3 - Print the range defined in the [From] and [To parameters]
  • 4 - Print the range of the [Pages parameter]

When you are not using one of the values, pass as EmptyParam.

To print a range of pages for example:

var
  Range, Pages: Olevariant;
begin
  ...
  Range := 4;
  Pages := '1;3-5;7;11;9';
  Doc.PrintOut(EmptyParam {Background}, EmptyParam {Append}, Range, EmptyParam 
    {OutputFileName}, EmptyParam {From}, EmptyParam {To}, EmptyParam {Item}, EmptyParam 
    {Copies}, Pages);

And if you want to use a range of pages From X to Y

var
  Range, From, To: Olevariant;
begin
  ...
  Range := 3;
  From := 3;
  To := 9;
  Doc.PrintOut(EmptyParam {Background}, EmptyParam {Append}, Range, EmptyParam 
    {OutputFileName}, From, To);
  • 1

    It worked. Thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.