【发布时间】:2013-07-31 21:44:10
【问题描述】:
我试图让 FreePascal 打开一个 word 文档,在其中附加一些文本和数据,然后关闭它。我已经成功连接,并且可以在文档中写一行,但任何超过这个的东西都让我失望。目前我正在尝试this Visual Basic reference 中的方法细节,这与我期望 FreePascal 处理事情的方式非常相似。
基本上我想我误解了 Lazarus 和 Word OLE 之间的关系实际上是如何工作的,谁能提供任何示例来说明如何构建一个可以构建的简单文档?
以下代码,打开文档,然后完全替换其内容
program officAuto;
{$IFDEF FPC}
{$MODE Delphi}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
SysUtils, Variants, ComObj;
const
ServerName = 'Word.Application';
var
Server, Doc : Variant;
oPara : Variant;
w:widestring;
begin
if Assigned(InitProc) then
TProcedure(InitProc);
try
Server := CreateOleObject(ServerName);
except
WriteLn('Unable to start Word.');
Exit;
end;
w:= UTF8Decode('c:\mydoc.docx');
Server.Visible := True; {Make Word visible}
Doc := Server.Documents.Open(w);
Doc.Range.Text := 'This is a Heading';
Doc.Range.Font.Bold := True;
Doc.Format.SpaceAfter := 24;
end.
然而,根据上面的代码,在尝试在书签上打印字符串时,打开文档,保留内容,移动到书签,然后什么都不做。
w:= UTF8Decode('c:\mydoc.docx');
Server.Visible := True;
Doc := Server.Documents.Open(w);
oPara := Doc.Content.Paragraphs.Add(Doc.Bookmarks.Item('\Bookmark1').Range);
oPara := Doc.Range.Text('Where will this appear if at all!');
【问题讨论】:
-
首先这是错误的! oPara := Doc.Range.Text('如果有的话,它会出现在哪里!');
标签: ms-word freepascal lazarus