Print OleObject ("word.application") with delphi

6

I'm trying to print a range of pages from objeto co_de OleObject to ("word.application") and I'm not getting it.

Using delphi 6 I can print successfully, but I want to print a range of pages.

Thank you very much for the 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;
    
asked by anonymous 24.06.2014 / 20:55

1 answer

3

The PrintOut method 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 Range property determines how page selection works according to the following values:

  • 0 - Print the entire document
  • 1 - prints the selected text
  • 2 - Prints the current page
  • 3 - Prints the interval 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 page range 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);
    
27.06.2014 / 17:09