How to add a paragraph to word using COMatePlus

Just starting out? Need help? Post your questions and find answers here.
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 234
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

How to add a paragraph to word using COMatePlus

Post by Ajm »

Hi,

I am trying to covert the following piece of VB code to use in COMatePlus and have become stuck at adding a paragraph at the end of the document.

Code: Select all

'Insert a paragraph at the end of the document.
'** \endofdoc is a predefined bookmark.
Set oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("\endofdoc").Range)
oPara2.Range.Text = "Heading 2"
oPara2.Format.SpaceAfter = 6
oPara2.Range.InsertParagraphAfter
I'm using the following code but get a Type Mismatch error when I run it in the debugger.
oPara2
2147614729
Type mismatch

Code: Select all

IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"

Define.COMateObject oWord, oDoc, oTable, oPara1, oPara2, oPara3, oPara4, oRng, oShape, oChart, oBookMark

Define pos.d

oWord = COMate_CreateObject("Word.Application")
If oWord
   oWord\SetProperty("Visible = #True")
   oDoc = oWord\GetObjectProperty("Documents\Add")
   
   ;Insert a paragraph at the beginning of the document.
   oPara1 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
   oPara1\SetProperty("Range\Text = 'Heading 1'")
   oPara1\SetProperty("Range\Font\Bold = True")
   oPara1\SetProperty("Format\SpaceAfter = 24")
   oPara1\SetProperty("Range\InsertParagraphAfter")
   
   ;Insert a paragraph at the end of the document.
   oBookMark = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
   If oBookMark
   Else 
      Debug "oBookMark"
      Debug COMate_GetLastErrorCode()
      Debug COMate_GetLastErrorDescription()
   EndIf 
   
   oPara2 = oDoc\GetObjectProperty("Content\Paragraphs\Add(" + Str(oBookMark) + " As Long)")
   If oPara2
      oPara2\SetProperty("Range\Text = 'Heading 2'")
      oPara2\SetProperty("Format\SpaceAfter = 6")
      oPara2\SetProperty("Range\InsertParagraphAfter")
   Else 
      Debug "oPara2"
      Debug COMate_GetLastErrorCode()
      Debug COMate_GetLastErrorDescription()
   EndIf 
   
   ; Free up the COM Objects
   oWord\Release()
   oDoc\Release()
   oPara1\Release()
   oBookMark\Release()
      
Else
   MessageRequester("COMatePlus - Test", "Couldn't create the oWord object!")
EndIf
I guessing I need to pass in the bookmark object (oBookMark) I have created when creating oPara2 but I'm struggling to work it out.
Can anyone who has used COMatePlus help me with this?

I'm using PB 5.60 Beta 6 x64 on Windows 10 x64.
Regards

Andy

Image
Registered PB & PureVision User
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: How to add a paragraph to word using COMatePlus

Post by srod »

Change " As Long" to " As COMateObject"
I may look like a mule, but I'm not a complete ass.
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 234
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Re: How to add a paragraph to word using COMatePlus

Post by Ajm »

Thanks srod.
Regards

Andy

Image
Registered PB & PureVision User
Post Reply