2013-07-31 1 views
0

Word 문서를 열어 FreePascal에 텍스트와 데이터를 추가 한 다음 닫으려고합니다. 나는 그럭저럭 연결될 수 있었고 그 문서에 한 줄을 쓸 수 있었지만 그 이상으로는 나를 물리 칠 수있다. 현재 this Visual Basic reference에 메서드 세부 정보를 시도하고 있는데, FreePascal에서 처리하는 방법과 매우 유사합니다.Lazarus FreePascal에서 Word OLE 사용하기

기본적으로 나는 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!'); 
+0

글쎄, 시작이 잘못되었습니다! oPara : = Doc.Range.Text ('전혀 보이지 않을 것입니다!'); – Funk247

답변

0

아 나는 그것을 해결했다. 다음 코드는 예상대로 작동합니다.

program officAuto; 

{$IFDEF FPC} 
    {$MODE Delphi} 
{$ELSE} 
    {$APPTYPE CONSOLE} 
{$ENDIF} 

uses 
    SysUtils, Variants, ComObj; 

var 
    Server, Connect : Variant; 
    oWord, oPara1, oPara2 : Variant; 

    w:widestring; 

    begin 
    if Assigned(InitProc) then 
    TProcedure(InitProc); 

    try 
    Server := CreateOleObject('Word.Application'); 
    except 
    WriteLn('Unable to start Word.'); 
    Exit; 
    end; 


    // oWord := Server.Documents.Add; 
    w:= UTF8Decode('c:\mydoc.docx'); 
    Server.Visible := True; 
    Server.Documents.Open(w); 

    oPara1 := Server.ActiveDocument.Content.Paragraphs.Add; 
    oPara1.Range.Text := 'This is a Heading'; 
    oPara1.Range.Font.Bold := True; 
    oPara1.Format.SpaceAfter := 24; 
    oPara1.Range.InsertParagraphAfter(); 

    oPara2 := Server.ActiveDocument.Content.Paragraphs.Add; 
    oPara2.Range.Text := 'Where will this appear if at all!'; 
    oPara2.Range.Font.Bold := False; 
    oPara2.Format.SpaceAfter := 24; 
    oPara2.Range.InsertParagraphAfter(); 
end. 
관련 문제