Create Word Doc and attach to Outlook Email in PowerBuilder
Here is some sample PowerScript from back in 2004. The word document created is saved from a template doc previously created with specific bookmarks used to format the text.
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<br />// get the Word template 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 // put data into bookmarks on document lole_word.activedocument.bookmarks.item('vendor').range.text = ids_xdex.getitemstring(1,'vendordesc') //data directly from datastore lole_word.activedocument.bookmarks.item('buyertext').range.text = ls_buyernote //data assigned previously to variable 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' //hard coded string data 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) // attach the word document lole_item.Display //displays the message // lole_item.Send //sends the message (commented out so user can personalize the message in Outlook if they want to) lole_outlook.disconnectobject() DESTROY lole_outlook DESTROY lole_word
Updated March 2021