14 Replies Latest reply on Sep 6, 2017 9:55 PM by robp32961061

    Export and attach it into a mail in 'Outlook'?

    RuyRamos

      Hello everybody,

       

      I have been looking around and trying to find ways to do the following with extendscript:

       

      1. Open 'Outlook'

      2. Create a new mail

      3. Attach a file to the mail

       

      Is this possible?

       

      I found this that dose it for MAC but I need to do it in a windows enviorment

      export and email pdf script

       

      Thanks for your help out there!

      Regards,

      Ruy Ramos

        • 1. Re: Export and attach it into a mail in 'Outlook'?
          Trevorׅ Adobe Community Professional

          Hi Ruy Ramos

           

          This will send the open document as an attachment through outlook.

          Please mark as correct, thanks.

           

          Enjoy

           

          Trevor

          // Send open doc as attachment through outlook on windows
          // By Trevor https://forums.adobe.com/thread/1474664
          // Need a custom script , willing to pay for it send me a private message.
          
          var notify = true; // set to true if you want a popup measage to say that the mail is sending
          var delay = 1.5 // the number of seconds you want the popup to appear
          var ToAddress = "Barack@White-house.org"; // You might need to change this
          
          
          var doc = app.properties.activeDocument && app.activeDocument;
          if (!doc || !doc.saved) {
              alert("Sorry mate I can't send your document as an attachment if it's not saved or does not exist\rPlease try again next year\rThanks");
              exit();
          }
          
          
          var MessageAttachment = doc.fullName.fsName;
              MessageSubject = "Howdy Barack, take a look at this"; // You might need to change this
              // the \" & vbNewLine & \" is a way off putting in line feads
              MessageBody = "Howdy i spent yonks of time making this document. \" & vbNewLine & \"I called it " + doc.name; // You might need to change this
          
          
          //VBS code very very very heavily based on the answer of ShaddowFox333 http://www.tek-tips.com/viewthread.cfm?qid=728333
          
          
          myVBS = '''Dim ToAddress
          Dim FromAddress
          Dim MessageSubject
          Dim MessageBody
          Dim MessageAttachment
          Dim ol, ns, newMail
          ToAddress = "''' + ToAddress + '''"
          MessageSubject = "''' + MessageSubject + '''"
          MessageBody = "''' + MessageBody + '''"
          MessageAttachment = "''' + MessageAttachment + '''"
          Set ol = CreateObject("Outlook.Application")
          Set ns = ol.getNamespace("MAPI")
          Set newMail = ol.CreateItem(olMailItem)
          newMail.Subject = MessageSubject
          newMail.Body = MessageBody & vbCrLf
          newMail.RecipIents.Add(ToAddress)
          newMail.Attachments.Add(MessageAttachment)
          newMail.Send
          
          '''
          
          try {
              app.doScript (myVBS, ScriptLanguage.VISUAL_BASIC, undefined, UndoModes.AUTO_UNDO);
              if (notify) {
                  var tempFile = new File (Folder.temp + "/" + +new Date + ".vbs");
                  tempFile.open('w');
                  tempFile.write('''Set shell = CreateObject("Wscript.Shell")
                              delay = ''' + delay + '''
                              shell.Popup "Sending document by outlook", delay, "InDesign Mail Manager", 64
                              Set fso = CreateObject("Scripting.FileSystemObject")
                              fso.DeleteFile("''' + tempFile.fsName + '''"),DeleteReadOnly
                              ''');
                  tempFile.close();
                  tempFile.execute();
              }
          }
          catch (e) {alert ("Drat goofed up")}
          
          
          
          • 2. Re: Export and attach it into a mail in 'Outlook'?
            Trevorׅ Adobe Community Professional

            Hi Ruy Ramos,

             

            Was there something not answered here?

            I thought the script I provided answered the question very well.

             

            How about "Thanks Trevor" and marking the question as answered.

             

            Trevor

            • 3. Re: Export and attach it into a mail in 'Outlook'?
              RuyRamos Level 1

              Hi Trevor,

               

              I'm so sorry about not replying you. I have the classic excuses that I have been working a lot and been busy. I haven't been able to test your solution yet but I will do soon. I hope you didin't get offended. I really appreciate your answer! THANK YOU!

              • 4. Re: Export and attach it into a mail in 'Outlook'?
                jakec88782761 Level 1

                Hi Trevor,

                I tried this but it says drat goofed up every time

                • 5. Re: Export and attach it into a mail in 'Outlook'?
                  Trevorׅ Adobe Community Professional

                  Hi Jake

                   

                  Which version of Windows and outlook did you goof on?

                   

                  Trevor

                  • 6. Re: Export and attach it into a mail in 'Outlook'?
                    jakec88782761 Level 1

                    Hi Trevor,

                     

                    Thanks for the reply

                     

                    I'm using Outlook 2010 and InDesign CC version 11.0

                    • 8. Re: Export and attach it into a mail in 'Outlook'?
                      Trevorׅ Adobe Community Professional

                      Try out the below and post the generated VBS and the error you get if you get an error.

                      I don't know if I'll have time to help but you can give it a go.

                       

                      // Send open doc as attachment through outlook on WINDOWS
                      // By Trevor https://forums.adobe.com/thread/1474664
                      // Need a custom script , willing to pay for it send me a private message.
                      
                      // Mac users be warned
                      if ($.os[0] === 'M') {
                          alert('JERK!');
                          exit();
                      }
                      
                      var notify = false; // set to true if you want a popup measage to say that the mail is sending
                      var delay = 1.5; // the number of seconds you want the popup to appear
                      var ToAddress = "Clinton@White-house.gov.us"; // You might need to change this
                          ToAddress = "Trump@White-house.gov.us"; // You might need to change this
                      
                      
                      var doc = app.properties.activeDocument && app.activeDocument;
                      if (!doc || !doc.saved) {
                          alert("Sorry mate I can't send your document as an attachment if it's not saved or does not exist\rPlease try again next year\rThanks");
                          exit();
                      }
                      
                      
                      var MessageAttachment = doc.fullName.fsName;
                      MessageSubject = "Howdy Barack, take a look at this"; // You might need to change this
                      // the \" & vbNewLine & \" is a way off putting in line feads
                      MessageBody = "Howdy i spent yonks of time making this document. \" & vbNewLine & \"I called it " + doc.name; // You might need to change this
                      
                      
                      //VBS code very very very heavily based on the answer of ShaddowFox333 http://www.tek-tips.com/viewthread.cfm?qid=728333
                      
                      
                      myVBS = [
                          'Dim ToAddress',
                          'Dim FromAddress',
                          'Dim MessageSubject',
                          'Dim MessageBody',
                          'Dim MessageAttachment',
                          'Dim ol, ns, newMail',
                          'ToAddress = "' + ToAddress + '"',
                          'MessageSubject = "' + MessageSubject + '"',
                          'MessageBody = "' + MessageBody + '"',
                          'MessageAttachment = "' + MessageAttachment + '"',
                          'Set ol = CreateObject("Outlook.Application")',
                          'Set ns = ol.getNamespace("MAPI")',
                          'Set newMail = ol.CreateItem(olMailItem)',
                          'newMail.Subject = MessageSubject',
                          'newMail.Body = MessageBody & vbCrLf',
                          'newMail.RecipIents.Add(ToAddress)',
                          'newMail.Attachments.Add(MessageAttachment)',
                          'newMail.Send'
                      ].join('\n');
                      
                      try {
                          // app.doScript (myVBS, ScriptLanguage.VISUAL_BASIC, undefined, UndoModes.AUTO_UNDO);
                          var tempFile = new File(Folder.temp + "/" + (+(new Date())) + ".vbs");
                          tempFile.open('w');
                          tempFile.write(myVBS);
                          tempFile.close();
                          tempFile.execute();
                          tempFile = new File(Folder.temp + "/" + (+(new Date()) + 1) + ".txt");
                              tempFile.open('w');
                              tempFile.write('Contents of vbs generated:\n\n' + myVBS);
                              tempFile.close();
                              tempFile.execute();
                      
                          if (notify) {
                              tempFile = new File(Folder.temp + "/" + (+(new Date())) + ".vbs");
                              tempFile.open('w');
                              tempFile.write(
                                  [
                                      'Set shell = CreateObject("Wscript.Shell")',
                                      'delay = ' + delay ,
                                      'shell.Popup "Sending document by outlook", delay, "InDesign Mail Manager", 64',
                                      'Set fso = CreateObject("Scripting.FileSystemObject")',
                                      'fso.DeleteFile("' + tempFile.fsName + '"), DeleteReadOnly'
                                  ].join('\n')
                              );
                              tempFile.close();
                              tempFile.execute();
                          }
                      } catch (e) {
                          alert("Drat goofed up");
                      }
                      
                      • 9. Re: Export and attach it into a mail in 'Outlook'?
                        jakec88782761 Level 1

                        Hi Trevor,

                         

                        This works perfectly thanks!

                        • 10. Re: Export and attach it into a mail in 'Outlook'?
                          jakec88782761 Level 1

                          Could you explain why there are so many quotes around the JavaScript variables in the VB script part?

                          • 11. Re: Export and attach it into a mail in 'Outlook'?
                            Trevorׅ Adobe Community Professional

                            I arranged all the separate lines in an array to be joined by the join('\n') so that one set of quotes per line, I have gone off using triple single quotes as the auto formatters and linters don't handle them. Besides that I think it's a bad practice.

                            'MessageBody = "' + MessageBody + '"'

                             

                            The blue big single quotes surround the VBS code

                            The small red double quotes are part of the vbs

                            The oranges MessageBody variable will be replaced by it's value when sent to the vbs

                            Got it? good.

                            How colorful

                            • 12. Re: Export and attach it into a mail in 'Outlook'?
                              jakec88782761 Level 1

                              Thanks Trevor.

                               

                              Is it possible to send a PDF version of the activedocument in Outlook?

                               

                              I've tried exporting during your script but Outlook doesn't find the PDF file.

                               

                              If it is possible I'd be willing to pay you

                              • 13. Re: Export and attach it into a mail in 'Outlook'?
                                Trevorׅ Adobe Community Professional

                                Always happy to take a bob or 2.

                                You can contact me through my contact page

                                Contact | Creative-Scripts.com

                                I think your problem is your trying to do an asynchronous export and not a synchronous one.

                                I'll be out for a while.

                                • 14. Re: Export and attach it into a mail in 'Outlook'?
                                  robp32961061

                                  Many thanks Trevor,

                                   

                                  Lovely script, worked a charm for me. I modified it to make it a universal function so I could use it to email status updates to various addresses while InDesign ran lengthy scripts on a schedule.

                                   

                                  Cheers,

                                  Rob