简体   繁体   English

使用OLE在MS Word中打印一系列页面(Imprimir em ole对象(word应用程序)com delphi)

[英]Printing a range of pages in MS Word using OLE (Imprimir em ole object (word application) com delphi)

I'm trying to print a range of pages from my Ole Object object, but it's not working for me - I get a "type mismatch" exception when the call to Doc.PrintOut executes. 我正在尝试从Ole Object对象打印一系列页面,但是它对我不起作用-执行对Doc.PrintOut的调用时出现“类型不匹配”异常。

Can anyone help me to avoid this error? 谁能帮助我避免此错误? Below is the code used in tests: 以下是测试中使用的代码:

  if (OpenDialog1.Execute) then
    begin
        // 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, Range, 1, 2);
                  //Doc.PrintOut(Copies := 2);
                  vFrom := 1;
                  vTo := 2;
                  Doc.PrintOut(Background := false, Append := false, Range :=       wdPrintFromTo, OutputFileName := EmptyParam, From := vFrom, To := vTo);
                  // erro recebido: tipo não correspondente
              finally
                  // Fecha o Word
                  WinWord.Quit;
           end;
    end;

    showmessage('Fim!');
end;

I tried your code in D7, using the English-language version of Office2007. 我使用英语版本的Office2007在D7中尝试了您的代码。

I get an exception error on Doc.PrintOut which is Delphi's usual "Type Mismatch". 我在Doc.PrintOut上收到异常错误,这是Delphi通常的“类型不匹配”。 By experimenting with the arguments passed to Doc.PrintOut, I think it's the 通过试验传递给Doc.PrintOut的参数,我认为这是

Range := wdPrintFromTo

that's causing the exception, because a "range" would normally be a block of text, whereas wdPrintFromTo is a numeric constant (but then, the Word macro I mention below uses a numeric constant for the range, so maybe Word just doesn't like th one you used). 这是造成异常的原因,因为“范围”通常是一块文本,而wdPrintFromTo是一个数字常量(但是,我下面提到的Word宏使用了一个数字常量作为范围,所以也许Word不喜欢您使用的那个)。

Then, I got Word to record a macro to print the first two pages of a 6-page doc, and found that I could then get your code to work by making these changes: 然后,我让Word记录一个宏,以打印6页文档的前两页,然后发现通过进行以下更改,我可以使您的代码正常工作:

  • replace vFrom and vTo, which I assume are OleVariants, by two integers iFrom and iTo (I don't think that this really matters, just makes it easier to check that the page-range argument is correctly constructed. 用两个整数iFrom和iTo替换vFrom和vTo(我认为是OleVariants)(我认为这并不重要,只是可以更轻松地检查是否正确构造了页面范围参数。

  • replace the code from "vFrom := [...] to Doc.PrintOut by 将代码从“ vFrom:= [...]替换为Doc.PrintOut”

     iFrom := 1; iTo := 2; Doc.PrintOut( Range := wdPrintRangeOfPages, Item := wdPrintDocumentContent, Copies := 1, Pages := IntToStr(iFrom) + '-' + IntToStr(iTo), PageType := wdPrintAllPages, ManualDuplexPrint := False, Collate := True, Background := True, PrintToFile := False, PrintZoomColumn := 0, PrintZoomRow := 0, PrintZoomPaperWidth := 0, PrintZoomPaperHeight := 0 ); 

Obviously, the arguments this PrintOut includes are ones which the Word macro included. 显然,此PrintOut包含的参数是Word宏所包含的参数。 and typically some of them would probably be superfluous. 通常其中一些可能是多余的。

Thanks for all! 谢谢大家! MartynA, Jan Doggen, David Heffernan, Mason Wheeler and all others!! MartynA,Jan Doggen,David Heffernan,Mason Wheeler等所有人!

I get solution with the code below: 我得到以下代码的解决方案:

<pre><code>
procedure TForm1.Button1Click(Sender: TObject);
var
  WinWord, Docs, Doc: Variant;
  vNomeImpressoraPadraoOriginal : string;
  vFrom, vTo : integer;
begin
  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);
          vFrom := 1;
          vTo := 2;
          // referência do comando: http://support.microsoft.com/kb/176069/EN-US
          Doc.PrintOut(0, 0, '3', '', '1', '2');
          // previous code
          // Doc.PrintOut(Background := false, Append := false, Range :=       wdPrintFromTo, OutputFileName := EmptyParam, From := vFrom, To := vTo);
        finally
          // Fecha o Word
          WinWord.ActiveDocument.Close(SaveChanges := 0);
          WinWord.Quit;

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

      showmessage('Fim!');
    finally

    end;
  end;
end;
</code></pre>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM