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!

Create a word doc and send it via Outlook

MS Word & Outlook OLE

Create a word doc and send it via Outlook

by  mbalent  Posted    (Edited  )
Here is some sample script. The word document created is saved from a template doc previously created with specific bookmarks used to format the text.
Code:
oleobject lole_word
OLEObject lole_item, lole_attach, lole_outlook
string ls_file_name

lole_word = CREATE oleobject
lole_outlook = Create OLEObject
TRY
	lole_word.connecttonewobject('word.application')
CATCH (runtimeerror a)
	Messagebox('Error','Error connecting with MS Word. Process terminating.')
	RETURN -1
END TRY
lole_word.visible = FALSE // dont want to see word

ls_file = C:\temp\template.doc

If Not FileExists(ls_file) Then
	//ERROR CONDITION
	 li_rc = MessageBox("File Not Found", "Cannot find document template: "+ls_file+"~r"+ "Do you want to select the template?", Question!, YesNo!, 2)
	 If li_rc = 2 Then
		Return -1
	 Else
		GetFileOpenName("Select Template File", ls_file, ls_file_name, ".DOC","Template Files, *.DOC")
		If Not FileExists(ls_file_name) Then
			// ERROR CONDITION
			MessageBox("Template File Not Found","Cannot find document: "+ ls_file_name)
		   return -1
		End If
	 End If
ELSE
	
End If

TRY
	lole_word.Documents.open(ls_file_name)
CATCH (runtimeerror b)
	Messagebox('Error','Error opening ' + ls_file_name + ' with MS Word. Process terminating.')
	RETURN -1
	
END TRY
lole_word.activedocument.bookmarks.item('vendor').range.text = ids_xdex.getitemstring(1,'vendordesc')

lole_word.activedocument.bookmarks.item('buyertext').range.text = ls_buyernote
lole_word.activedocument.bookmarks.item('text').range.text = ls_data
lole_word.activedocument.bookmarks.item('legend').range.text = '* D - De Expedite, E - Expedite, C - Cancel'
lole_word.activedocument.bookmarks.item('buyer').range.text = ls_name
lole_word.activedocument.bookmarks.item('buyerfax').range.text = ls_fax
lole_word.activedocument.bookmarks.item('buyeremail').range.text = ls_email


is_doc = ls_file + ls_doc
// save document before processing further
TRY
	lole_word.activedocument.saveas(is_doc)
CATCH (runtimeerror c)
	Messagebox('Error','Error saving document ' + is_doc + '. Process terminating.')
	RETURN -1
END TRY
TRY
	lole_word.activedocument.saveas('P:\temp.doc') // save 'dummy' so 'real' document is not locked
CATCH (runtimeerror d)
	Messagebox('Error','Error saving temp document. Process terminating.')
	RETURN -1
END TRY

lole_word.activedocument.printout()	// print from word


//Connect to Outlook session using 'Outlook.Application'
li_rc = lole_outlook.ConnectToNewObject("outlook.application")

If li_rc <> 0 Then
	// ERROR CONDITION
          Messagebox("Outlook Error",string(li_rc))
          Destroy lole_outlook
          Return li_rc
End If
//Creates a new mail Item
lole_item = lole_outlook.CreateItem(0)
//Set the subject line of message
lole_item.Subject = "Expedite / De Expedite Notice"
//Body of mail message
lole_item.Body = "Please review the attached file and advise: "+Char(13)
//Recipient(s) Use a semicolon to separate multiple recipients
lole_item.To = dw_vendorfax.getitemstring(1,'contactemail')
lole_attach = lole_item.Attachments
lole_attach.add(is_doc)
lole_item.Display //displays the message
//    lole_item.Send //sends the message
lole_outlook.disconnectobject()

DESTROY lole_outlook
DESTROY lole_word
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top