Insert text file into Word doc with COMatePLUS?

Just starting out? Need help? Post your questions and find answers here.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Insert text file into Word doc with COMatePLUS?

Post by MachineCode »

I'm trying to programmatically insert a text file into a Word doc. Here's some code I found, and what I've done. I keep getting an error with the "InsertFile" command though ("One or more arguments are invalid"). Does anyone have a clue? Thanks.

Original code:

Code: Select all

Dim wordApp As Word.Application
Dim wordDoc As Word.Document

Set wordApp = New Word.Application
wordApp.Documents.Add
Set wordDoc = wordApp.ActiveDocument

wordDoc.Content.InsertAfter vbCrLf & "---------------begin line--------------" & vbCrLf

wordDoc.Content.InsertFile App.Path & "insert.doc", "", False, False, False

wordDoc.Content.InsertAfter vbCrLf & "---------------end line--------------" & vbCrLf

'MsgBox App.Path
wordDoc.SaveAs App.Path & "file.doc"
wordDoc.Application.Quit
Set wordDoc = Nothing
Set wordApp = Nothing
My pathetic effort (modded slightly from another example in these forums):

Code: Select all

IncludePath #PB_Compiler_Home+"COMatePLUS\"
XIncludeFile "COMatePLUS.pbi"

Procedure ProtectDocument()

  Protected.S strCommand
  Protected appWord.COMateObject, docMine.COMateObject, objBookmark.COMateObject, objRange.COMateObject

  appWord = COMate_CreateObject("Word.Application")

  If appWord

    appWord\Invoke("Documents\Open('c:\test.doc')")
    docMine = appWord\GetObjectProperty("ActiveDocument")
    docMine\Invoke("InsertFile(FileName:='c:\data.txt'")
    Debug COMate_GetLastErrorDescription()

    docMine\Release()
    appWord\Release()

  EndIf

EndProcedure

ProtectDocument()
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
spikey
Enthusiast
Enthusiast
Posts: 771
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Insert text file into Word doc with COMatePLUS?

Post by spikey »

1) You haven't terminated the open parenthesis inside the quotes in the line : docMine\Invoke("InsertFile(FileName:='c:\data.txt'")

2) As ActiveDocument is a pointer to the current object in the Documents() collection, this won't work anyway. InsertFile is a member of the Selection, Content or Range objects but NOT the Document object directly. A correct syntax would be ActiveDocument.Content.InsertFile, which you can see in the VBA you posted.

3) Incidentally, even if this had worked, you wouldn't see the results. A new instance of Word created using CreateObject will have the Application.Visible property set to False by default (or at least it does in my version of Word).

4) Another incidentally, because of the way word does things your code would REPLACE the current content of the opened document with the content in the file - I'm not sure if this is what you intended?
leodh
Enthusiast
Enthusiast
Posts: 164
Joined: Sun Nov 06, 2005 6:07 am
Location: Perth Western Australia

Re: Insert text file into Word doc with COMatePLUS?

Post by leodh »

Hi,

Sorry to drag up an old post but I am trying to do the same ( insert one word Doc into an other ) but I am stuck.

It uses srod Comate Plus.

I have tried to impliment the changes that spikey listed for the code above, but it still does not work.

I still get the following error
"One or more arguments are invalid. Possibly a numerical overflow or too many nested objects, -if so, try splitting your method call into two or more subcalls."

This is the line that causes the error

Code: Select all

docMine\Invoke("InsertFile FileName = '" + "c:\data.doc" + "'")
I have tryied several versions of this line all to no avail

Has anyone been able to get this to work, if so how?

There are several simple worked exampled in C or VB but I was not able to translate them into Comate Plus speak.

They all seem to be a slightly different version of the code below ( Macro from Word )

Code: Select all

Selection.InsertFile FileName:=  "X:\AHL Software\Worksheet.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False

Regards Leo
Regards
Leo
Post Reply