从Delphi操纵MS Word打开对话框

问题描述 投票:0回答:2

每次运行此代码时,我都会手动确认要转换“ PDF文件”。

有没有办法使它自动化?

procedure TFWordAnalyzer.Button1Click(Sender: TObject);
var
  Clipboard:TClipboard;
  WordApp,WordDocument: OleVariant;
begin
  try
    WordApp := GetActiveOleObject('Word.Application');
  except
    WordApp := CreateOleObject('Word.Application') ;
 end;

  WordApp.visible := true;
  WordApp.DisplayAlerts := False;

  WordDocument := WordApp.Documents.Open('D:\Corpus\Adyg\Hudmir.pdf', true, false);

  WordDocument.range.copy;

          sleep(1000);//Otherwise it fails
          RichEdit1.Clear;
          RichEdit1.PasteFromClipboard;

  WordDocument.close;
  WordApp.Quit;
  WordApp := Unassigned;
end;

enter image description here

delphi ms-word dialog
2个回答
1
投票

尝试更新打开文档的代码行

WordDocument := WordApp.Documents.Open('D:\Corpus\Adyg\Hudmir.pdf', ConfirmConversions := False);

这将在不调用“确认转换”对话框的情况下打开文档。

假定可行,您应该能够恢复第二和第三个参数,这样您就可以了

WordDocument := WordApp.Documents.Open('D:\Corpus\Adyg\Hudmir.pdf', true, false, ConfirmConversions := False);

注意语法

WordApp.Documents.Open([...], ConfirmConversions := False)

通过OleVariants与OLE自动化同时(在D2中)同时添加,以支持将可选参数传递给自动化调用。

PS:进行此更改后,您可能会发现不必要打入睡眠电话。我之所以说“可能”,是因为我的测试代码首先不需要它。

Update OP报告说上面的代码可能会导致提示字词收尾时表示剪贴板上有大量数据,并询问是否可以自动响应提示。

Google发现了一些避免提示的建议,但我发现没有一个建议他们中的一个可靠地工作与17Mb .Pdf文件。方法如下图所示,我是通过实验得出的,does似乎可以可靠地工作,但并不是那么简单如我所愿。

出现提示的原因是Word显然设置了内部指针提供给剪贴板的内容,所以我想知道是否设置将范围缩小到更小的东西,然后在关闭文档之前将其丢弃会工作。我遇到了两个问题:

  • 正在调用vRange.End:= 1无效

  • 试图调用vRange.Set_Text:=''引发了一个异常,声称该vRange尽管Range中的Range接口不支持Set_Text方法MS2000的Word2000.Pas导入单元显然可以。因此,下面的代码vRange变体中包含的Range对象,并对其调用Set_Text。这有效但意味着您需要添加Word2000.Pas(或更新的Word导入之一)单位)添加到Uses子句。

请注意,我删除了对GetActiveOle对象的调用,以避免出现异常失败时触发,中断了调试。

procedure TForm1.Button1Click(Sender: TObject);
var
  Clipboard:TClipboard;
  WordApp,
  WordDocument: OleVariant;
  vRange : OleVariant;
  iRange : Range;  //  or WordRange for D10.3.1+
begin
  try
    WordApp := CreateOleObject('Word.Application') ;
    WordApp.visible := true;
    WordApp.DisplayAlerts := False;

    try
      WordDocument := WordApp.Documents.Open('D:\aaad7\aaaofficeauto\test.pdf', ConfirmConversions := False); //, true, false);

      vRange := WordDocument.range;
      Label1.Caption := IntToStr(vRange.Start) + ':' + IntToStr(vRange.End);
      vRange.copy;

      sleep(1000);//Otherwise it fails
      RichEdit1.Clear;
      RichEdit1.PasteFromClipboard;

      //  vRange.Set_Text(' ');  // This provokes a "Method Set_Text not supported by automation object" exception
      //  SO ...
      //  Pick up the Range interface contained in the vRange variant
      iRange := IDispatch(WordDocument.range) as Range;
      // and call Set_Text on it to set the Range's text to a single space
      iRange.Set_Text(' ');
      //  Clear the iRange interface
      iRange := Nil;

      finally
        //   beware that the following discards and changes to the .Pdf
        //   so if you want to save any changes, you should do it
        //   before calling iRange.Set_Text
        WordDocument.close(False, False, False);
        WordDocument := UnAssigned;
      end;
  finally
    WordApp.Quit(False, False, False);
    WordApp := Unassigned;
  end;
end;

[已在Windows 10 64位/上使用MS Word 2010测试过


0
投票

这里是工作代码:

procedure TFWordAnalyzer.Button5Click(Sender: TObject);
var
  Clipboard:TClipboard;
WordApp,
WordDocument: OleVariant;
vRange : OleVariant;
iRange : WordRange;

begin

try
WordApp := CreateOleObject('Word.Application') ;
WordApp.visible := true;
WordApp.DisplayAlerts := False;

try
  WordDocument := WordApp.Documents.Open('D:\Corpus\Adyg\Hudmir.pdf', ConfirmConversions := False); //, true, false);

  vRange := WordDocument.range;
  Label1.Caption := IntToStr(vRange.Start) + ':' + IntToStr(vRange.End);
  vRange.copy;

  sleep(1000);//Otherwise it fails
  RichEdit1.Clear;
  RichEdit1.PasteFromClipboard;
  iRange := IDispatch(WordDocument.range) as WordRange;
  iRange.Set_Text(' ');
  iRange := Nil;

  finally

    WordDocument.close(False, False, False);
    WordDocument := UnAssigned;
  end;
 finally
   WordApp.Quit(False, False, False);
   WordApp := Unassigned;

 end;

  end;
© www.soinside.com 2019 - 2024. All rights reserved.