Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datawindow to MS Word Problem

Status
Not open for further replies.

girls3dog1

Programmer
Nov 22, 2002
30
US

Pb8

I have a problem with the following code that converts dw to an MS Word
file. I get an error message that states the following: "Error
calling external object function execute at line 74." Line 74 is the
mailmerge.execute, which is shown below. This is my first attempt at
ole and maybe I have the testdocument.doc set up incorrectly. I believe the error is generated by the fact that when my word template file "c:\database\testdocument2.dot" opens it does not have the datasource file attached for some reason. Thanks
in advance for any help provided.


///////////////////////////////////
////// Datawindow data to Word ////
///////////////////////////////////


pointer oldpointer // Declares a pointer variable
oldpointer = SetPointer(HourGlass!)

//Define ole object and Connect to Word
OleObject ole_Word

integer li_connect_result, ll_field_count
string ls_word_data, ls_document_name, ls_letter_data
string ls_insttag, ls_moduletag, ls_signaltag

ole_Word = Create OleObject

li_connect_result = ole_Word.ConnectToNewObject('word.application.10')

If li_connect_result <> 0 Then
Destroy ole_Word
Messagebox("Ole Error","Unable to connect Microsoft Word.")
Return
End if


//Get data from datawindow and place it in Word datasource file
c:\database\test1.doc
Datawindowchild dw_child
dw_io_list.GetChild('dw_1',dw_child)

ls_insttag = dw_child.getitemstring(1,'tbl_instrument_list_insttag')
ls_moduletag = dw_child.getitemstring(1,'tbl_io_list_controlmodule')
ls_signaltag = dw_child.getitemstring(1,'tbl_io_list_signaltag')

ls_letter_data = "Inst_tag,Module_tag,Signal_tag" + " ~t" + trim
(ls_insttag) + ", " + trim(ls_moduletag) + ", " + trim(ls_signaltag)


For ll_field_count = 2 to dw_child.rowcount( )

ls_insttag = dw_child.getitemstring
(ll_field_count,'tbl_instrument_list_insttag')
ls_moduletag = dw_child.getitemstring
(ll_field_count,'tbl_io_list_controlmodule')
ls_signaltag = dw_child.getitemstring
(ll_field_count,'tbl_io_list_signaltag')

ls_letter_data = ls_letter_data + "~t" + trim(ls_insttag) + ", " +
trim(ls_moduletag) + ", " + trim(ls_signaltag)

Next

ls_letter_data = ls_letter_data + "~t"+ "~r"

ole_Word.Application.Visible = True

ls_document_name = "c:\database\test1.doc"

ole_Word.Documents.Open(ls_document_name)

ole_Word.Application.Selection.WholeStory

ole_Word.Selection.TypeText(ls_letter_data)

ole_Word.Application.Selection.Delete(1,1)

ole_Word.ActiveDocument.SaveAs(ls_document_name)

ole_Word.ActiveDocument.Close(0)


//Open document and merge new data and save
ls_document_name = "c:\database\testdocument2.dot"

ole_Word.Documents.Open(ls_document_name)

ole_Word.ActiveDocument.MailMerge

ole_Word.ActiveDocument.MailMerge.Destination = 1

(Line 74) ole_Word.ActiveDocument.MailMerge.Execute(False) (Line 74)


ole_Word.DisconnectObject()

Destroy ole_Word

SetPointer(oldpointer)
 
Here is some sample code from the net. Google on 'mailmerge.execute'.

Code:
Example

[C#]

//Fill the fields in the document with user data.
doc.MailMerge.Execute(
	new string[] {"FullName", "Company", "Address", "Address2", "City"},
	new object[] {fullNameEdit.Text, companyEdit.Text, addressEdit.Text, address2Edit.Text, cityEdit.Text});

//Save the document in Word format.
doc.Save("PersonalizedLetter.doc", SaveFormat.FormatDocument);

[Visual Basic]

'Fill the fields in the document with user data.
doc.MailMerge.Execute(
	New String() {"FullName", "Company", "Address", "Address2", "City"},
	New Object() {fullNameEdit.Text, companyEdit.Text, addressEdit.Text, address2Edit.Text, cityEdit.Text})

'Save the document in Word format.
doc.Save("PersonalizedLetter.doc", SaveFormat.FormatDocument)

Looks to me like you need to send the method the fields and data.

Matt

"Nature forges everything on the anvil of time
 
Thanks, but I believe the problem lies in the fact that the following line of code does not open the document file with datasource file attached. If I step through the code in debug I can force it to open correctly and then mailmerge works correctly. I just don't know why this line of code will not open the doc file correctly.

ole_Word.Documents.Open(ls_document_name)
 
Permissions on the folder/file perhaps?

"Nature forges everything on the anvil of time
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top