Recent

Author Topic: Editing MS Word within Lazarus  (Read 906 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1449
Editing MS Word within Lazarus
« on: August 22, 2024, 06:03:11 am »
Hello,

I'm testing following codes which have been posted here. Following codes work fine.
Now I'd like to define "Style" of oPara2. But the "commented out" code does not execute.
It says Style is not reference property. But "style" is a legitimate property of word object Paragraph, which is the object of oPara2.

Does anybody know what I'm missing?

https://learn.microsoft.com/en-us/office/vba/api/word.paragraph.style

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   Server, Connect : Variant;
  4.   oWord, oPara1, oPara2 : Variant;
  5.  
  6.   w:widestring;
  7.  
  8. begin
  9.     if Assigned(InitProc) then
  10.     TProcedure(InitProc);
  11.  
  12.   try
  13.     Server := CreateOleObject('Word.Application');
  14.   except
  15.     ShowMessage('Unable to start Word.');
  16.     Exit;
  17.   end;
  18.  
  19.  
  20.   oWord := Server.Documents.Add;
  21.   Server.Visible := True;
  22.   // w := UTF8Decode('d:\temp\mydoc.docx');
  23.   // Server.Documents.Open(w);
  24.  
  25.   oPara1 := Server.ActiveDocument.Content.Paragraphs.Add;
  26.   oPara1.Range.Text := 'This is a Heading';
  27.   oPara1.Range.Font.Bold := True;
  28.   oPara1.Format.SpaceAfter := 24;
  29.   oPara1.Range.InsertParagraphAfter();
  30.  
  31.   oPara2 := Server.ActiveDocument.Content.Paragraphs.Add;
  32.   oPara2.Range.Text := 'Where will this appear if at all!';
  33.   oPara2.Range.Font.Bold := False;
  34.   oPara2.Format.SpaceAfter := 24;
  35.   oPara2.Range.InsertParagraphAfter();
  36.   // oPara2.Style := oWord.Styles(1);
  37. end;

loaded

  • Hero Member
  • *****
  • Posts: 852
Re: Editing MS Word within Lazarus
« Reply #1 on: August 22, 2024, 08:17:08 am »
I hope the codes below will work for you. By the way, there is no problem with your code. This error is related to the Lazarus version.  :)

Code: Pascal  [Select][+][-]
  1. uses ComObj;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   Server, Connect : Variant;
  6.   oWord, oPara1, oPara2 : Variant;
  7.   w:widestring;
  8.   style:integer=-2; //<----
  9. begin
  10.    if Assigned(InitProc) then TProcedure(InitProc);
  11.   try
  12.     Server := CreateOleObject('Word.Application');
  13.   except
  14.     ShowMessage('Unable to start Word.');
  15.     Exit;
  16.   end;
  17.   oWord := Server.Documents.Add;
  18.   Server.Visible := True;
  19.  
  20.   oPara1 := Server.ActiveDocument.Content.Paragraphs.Add;
  21.   oPara1.Range.Text := 'This is a Heading';
  22.   oPara1.Range.Font.Bold := True;
  23.   oPara1.Format.SpaceAfter := 24;
  24.   oPara1.Range.InsertParagraphAfter();
  25.  
  26.   oPara2 := Server.ActiveDocument.Content.Paragraphs.Add;
  27.   oPara2.Range.Text := 'Where will this appear if at all!';
  28.   oPara2.Range.Font.Bold := False;
  29.   oPara2.Format.SpaceAfter := 24;
  30.   oPara2.Style := style;
  31.   oPara2.Range.InsertParagraphAfter();
  32.  
  33. end;
  34.      
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

egsuh

  • Hero Member
  • *****
  • Posts: 1449
Re: Editing MS Word within Lazarus
« Reply #2 on: August 22, 2024, 09:40:08 am »
@loaded

Thank you for your comment. I can assign only integer values to the style property.
I found Word built-in style table. So, I can use these constants.

https://learn.microsoft.com/en-us/office/vba/api/word.wdbuiltinstyle

Zvoni

  • Hero Member
  • *****
  • Posts: 2690
Re: Editing MS Word within Lazarus
« Reply #3 on: August 22, 2024, 11:53:00 am »
@loaded

Thank you for your comment. I can assign only integer values to the style property.
I found Word built-in style table. So, I can use these constants.

https://learn.microsoft.com/en-us/office/vba/api/word.wdbuiltinstyle
That's what i just wanted to write, too.

Your Problem was, that the values for that enumeration are all negative, so your
oWord.Styles(1) failed because there is no "1" in that collection
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018