{"id":19,"date":"2009-09-23T10:47:43","date_gmt":"2009-09-23T15:47:43","guid":{"rendered":"http:\/\/anvil-of-time.com\/2009\/09\/23\/create-word-doc-and-attach-to-outlook-email-in-powerbuilder\/"},"modified":"2021-03-08T10:20:48","modified_gmt":"2021-03-08T15:20:48","slug":"create-word-doc-and-attach-to-outlook-email-in-powerbuilder","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/create-word-doc-and-attach-to-outlook-email-in-powerbuilder\/","title":{"rendered":"Create Word Doc and attach to Outlook Email in PowerBuilder"},"content":{"rendered":"<p>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.<\/p>\n<pre name=\"code\" class=\"VB\">oleobject lole_word\nOLEObject lole_item, lole_attach, lole_outlook\nstring ls_file_name\nlole_word = CREATE oleobject\nlole_outlook = Create OLEObject\nTRY\n\tlole_word.connecttonewobject(&#039;word.application&#039;)\nCATCH (runtimeerror a)\n\tMessagebox(&#039;Error&#039;,&#039;Error connecting with MS Word. Process terminating.&#039;)\n\tRETURN -1\nEND TRY\nlole_word.visible = FALSE \/\/ dont want to see word&lt;br \/&gt;\/\/ get the Word template\nls_file = C:\\temp\\template.doc\nIf Not FileExists(ls_file) Then\n\t\/\/ERROR CONDITION\n\t li_rc = MessageBox(&quot;File Not Found&quot;, &quot;Cannot find document template: &quot;+ls_file+&quot;~r&quot;+ &quot;Do you want to select the template?&quot;, Question!, YesNo!, 2)\n\t If li_rc = 2 Then\n\t\tReturn -1\n\t Else\n\t\tGetFileOpenName(&quot;Select Template File&quot;, ls_file, ls_file_name, &quot;.DOC&quot;,&quot;Template Files, *.DOC&quot;)\n\t\tIf Not FileExists(ls_file_name) Then\n\t\t\t\/\/ ERROR CONDITION\n\t\t\tMessageBox(&quot;Template File Not Found&quot;,&quot;Cannot find document: &quot;+ ls_file_name)\n\t\t   return -1\n\t\tEnd If\n\t End If\nELSE\nEnd If\nTRY\n\tlole_word.Documents.open(ls_file_name)\nCATCH (runtimeerror b)\n\tMessagebox(&#039;Error&#039;,&#039;Error opening &#039; + ls_file_name + &#039; with MS Word. Process terminating.&#039;)\n\tRETURN -1\nEND TRY\n\/\/ put data into bookmarks on document\nlole_word.activedocument.bookmarks.item(&#039;vendor&#039;).range.text = ids_xdex.getitemstring(1,&#039;vendordesc&#039;) \/\/data directly from datastore \nlole_word.activedocument.bookmarks.item(&#039;buyertext&#039;).range.text = ls_buyernote \/\/data assigned previously to variable\nlole_word.activedocument.bookmarks.item(&#039;text&#039;).range.text = ls_data\nlole_word.activedocument.bookmarks.item(&#039;legend&#039;).range.text = &#039;* D - De Expedite, E - Expedite, C - Cancel&#039; \/\/hard coded string data\nlole_word.activedocument.bookmarks.item(&#039;buyer&#039;).range.text = ls_name\nlole_word.activedocument.bookmarks.item(&#039;buyerfax&#039;).range.text = ls_fax\nlole_word.activedocument.bookmarks.item(&#039;buyeremail&#039;).range.text = ls_email\nis_doc = ls_file + ls_doc\n\/\/ save document before processing further\nTRY\n\tlole_word.activedocument.saveas(is_doc)\nCATCH (runtimeerror c)\n\tMessagebox(&#039;Error&#039;,&#039;Error saving document &#039; + is_doc + &#039;. Process terminating.&#039;)\n\tRETURN -1\nEND TRY\nTRY\n\tlole_word.activedocument.saveas(&#039;P:\\temp.doc&#039;) \/\/ save &#039;dummy&#039; so &#039;real&#039; document is not locked\nCATCH (runtimeerror d)\n\tMessagebox(&#039;Error&#039;,&#039;Error saving temp document. Process terminating.&#039;)\n\tRETURN -1\nEND TRY\nlole_word.activedocument.printout()\t\/\/ print from word\n\/\/Connect to Outlook session using &#039;Outlook.Application&#039;\nli_rc = lole_outlook.ConnectToNewObject(&quot;outlook.application&quot;)\nIf li_rc &amp;lt;&amp;gt; 0 Then\n\t\/\/ ERROR CONDITION\n          Messagebox(&quot;Outlook Error&quot;,string(li_rc))\n          Destroy lole_outlook\n          Return li_rc\nEnd If\n\/\/Creates a new mail Item\nlole_item = lole_outlook.CreateItem(0)\n\/\/Set the subject line of message\nlole_item.Subject = &quot;Expedite \/ De Expedite Notice&quot;\n\/\/Body of mail message\nlole_item.Body = &quot;Please review the attached file and advise: &quot;+Char(13)\n\/\/Recipient(s) Use a semicolon to separate multiple recipients\nlole_item.To = dw_vendorfax.getitemstring(1,&#039;contactemail&#039;)\nlole_attach = lole_item.Attachments\nlole_attach.add(is_doc) \/\/ attach the word document\nlole_item.Display \/\/displays the message\n\/\/    lole_item.Send \/\/sends the message (commented out so user can personalize the message in Outlook if they want to)\nlole_outlook.disconnectobject()\nDESTROY lole_outlook\nDESTROY lole_word<\/pre>\n\n\n<p>Updated March 2021<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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(&#039;word.application&#039;) CATCH (runtimeerror a) Messagebox(&#039;Error&#039;,&#039;Error connecting with MS Word.&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,10],"tags":[12,13,16],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/19"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":11,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":1713,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/19\/revisions\/1713"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}