Announcement

Collapse
No announcement yet.

Does anyone know hot to open MS Outlook programmaticly and pass data in?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does anyone know hot to open MS Outlook programmaticly and pass data in?

    Does anyone know hot to open MS Outlook programmaticly and pass data in? I am trying to start outlook and pass in the subject and body. OLE? Wondering where I should start my search...

  • #2
    You might need to look at Outlook Redemption for some of the stuff you want to do... either way a really good resource.
    <b>George W. Bleck</b>
    <img src='http://www.blecktech.com/myemail.gif'>

    Comment


    • #3
      Search for MAPI

      here is some code that works with outlook 2010, don't know if it works with newer versions.

      Code:
      #COMPILE EXE
      #DIM ALL
      
      #IF NOT %DEF(%TRUE)
      %TRUE   = 1
      #ENDIF
      #IF NOT %DEF(%FALSE)
      %FALSE  = 0
      #ENDIF
      #IF NOT %DEF(%NULL)
      %NULL   = 0
      #ENDIF
      
          %olFormatUnspecified = 0
          %olFormatPlain = 1
          %olFormatHTML = 2
          %olFormatRichText = 3
      
          %olMailItem = 0
      
          %olImportanceLow = 0
          %olImportanceNormal = 1
          %olImportanceHigh = 2
      
      
      %DisplaySend = -1       ' -1 Display, else Send
      
      FUNCTION PBMAIN () AS LONG
      LOCAL saMapiProf () AS STRING
      LOCAL lErrCode AS LONG
      LOCAL lResult AS LONG
      
       SendOutlookMail ("To@mail.com; other@mail.net", "cc@mail.net", "my subject", "body text", %olFormatPlain, %olImportanceNormal, %DisplaySend)
      
      END FUNCTION
      
      '=============================================================================
      ' SendOutlookMail() Function using late-binding and minimal error checking
      '=============================================================================
      FUNCTION SendOutlookMail( BYVAL sEmailTo     AS WSTRING, _
                                BYVAL sEmailCC     AS WSTRING, _
                                BYVAL sSubject     AS WSTRING, _
                                BYVAL sBodyText    AS WSTRING, _
                                BYVAL lBodyFormat  AS LONG,   _
                                BYVAL lImportance  AS LONG,   _
                                BYVAL lDisplayOnly AS LONG _
                                ) EXPORT AS LONG
      
         LOCAL oOutlook   AS DISPATCH
         LOCAL oMessage   AS DISPATCH
         LOCAL oRecipient AS DISPATCH
         LOCAL oAttach    AS DISPATCH
      
         LOCAL sName AS WSTRING
         LOCAL i     AS LONG
      
         LOCAL vVar  AS VARIANT
         LOCAL vTemp AS VARIANT
      
         '===========================
         ' Create an outlook session
         '
         oOutlook = NEWCOM "Outlook.Application"
         IF ISFALSE ISOBJECT(oOutlook) THEN
            ? "Outlook Object Could not be created", %MB_OK, "Error"
            FUNCTION = %FALSE
            EXIT FUNCTION
         END IF
      
      
         '================================
         ' Create a Message
         '
         LET vTemp = %olMailItem
         OBJECT CALL oOutlook.CreateItem(vTemp) TO oMessage
         IF ISNOTHING(oMessage) THEN ? "oMessage failed. err=" & STR$(ERR)
      
      
         '==================================
         ' Create Recipient
         '
            sName = sEmailTo
            LET vTemp = sName
            OBJECT LET oMessage.TO = vTemp
      
      
         ' Add a CC if specified
               sName = sEmailCC
               LET vTemp = sName
               OBJECT LET oMessage.CC = vTemp
      
         ' Add the email Subject
         LET vTemp = sSubject
         OBJECT LET oMessage.Subject = vTemp
      
      
         ' Add the email body based on the format
         LET vTemp = sBodyText
         SELECT CASE lBodyFormat
            CASE %olFormatUnspecified, %olFormatPlain
               OBJECT LET oMessage.Body = vTemp
            CASE %olFormatHTML
               OBJECT LET oMessage.HTMLBody = vTemp
            CASE %olFormatRichText
               OBJECT LET oMessage.RTFBody = vTemp
         END SELECT
      
      
         ' Set the email importance
         LET vTemp = lImportance
         OBJECT LET oMessage.Importance = vTemp
      
      
         '=======================================================================
         ' Display Message Before Sending
         '
         IF ISTRUE(lDisplayOnly) THEN
            OBJECT CALL oMessage.Display
         ELSE
            OBJECT CALL oMessage.Save
            OBJECT CALL oMessage.Send
            '? "Outlook Email has been sent.", %MB_OK, "Success"
         END IF
      
         SET oOutlook = NOTHING
         FUNCTION = %TRUE
      
      END FUNCTION

      Comment


      • #4
        Thanks everybody! Very helpful!

        Comment


        • #5
          Would someone please test if this code will attach a file using Outlook 2010 or Outlook?
          I read somewhere Microsoft products allow attach to work with Shell.
          Code:
          #INCLUDE "win32api.inc"
          FUNCTION PBMAIN () AS LONG 'ShellEmail.bas
           LOCAL zAttach AS ASCIIZ * 128
           LOCAL zText   AS ASCIIZ * 256
           zAttach = "test.txt"
           OPEN zAttach FOR OUTPUT AS #1
           PRINT #1, "A line of text"
           CLOSE #1
           IF ISFALSE(ISFILE(zAttach)) THEN
             ? zAttach + $CR + "Attachment file missing",%MB_SYSTEMMODAL,EXE.NAME$
             EXIT FUNCTION
           END IF
           zText = "mailto:" + "?"
           zText+= "&Subject=" + "Document attached"
           zText+= "&Body="    + "Please find the document attached"
           zText+= "&Attach="  + zAttach
           ShellExecute (%NULL, "OPEN", zText, BYVAL %NULL, CURDIR$, %SW_SHOWNORMAL)
          END FUNCTION
          https://duckduckgo.com instead of google

          Comment


          • #6
            Originally posted by Mike Doty View Post
            Would someone please test if this code will attach a file using Outlook 2010 or Outlook?
            I read somewhere Microsoft products allow attach to work with Shell.
            Hi Mike,
            I have Outlook 2010 and everything worked except the attachment.
            I tried fully qualifying the attached file and with/without double quotes
            Googling it, the consensus was that attachments may have worked for older Office versions (2000?).
            An alternative, CDO, works well for me - but the flip side is that the sent email does not get stored in Sent Items - which may or may not suit your needs.
            Failing that, the MAPI interface would be the way to go. It's likely you know all this and are just looking for a simple Shellexecute solution. It would be handy.
            Regards, Ross

            Comment


            • #7
              I have a client with Windows 2010 and he wanted everything to go through Outlook.
              Forgot about the send folder, good grief, just finished a program to send without it
              Thank you!
              https://duckduckgo.com instead of google

              Comment


              • #8
                Wound up doing it this way:
                Code:
                <a class="emailButton" href="mailto:?subject=This is the subject&body=This is the body. Please add card number 3865098 for Ken Cole.%0a%0aThanks!%0aDavid%0a">Email</a>

                Comment


                • #9
                  Is that a shellexecute command?
                  Code:
                  zText = "mailto:?subject=This is the subject&body=This is the body. Please add card number 3865098 for Ken Cole.%0a%0aThanks!%0aDavid%0a"
                  ShellExecute (%NULL, "OPEN", zText, BYVAL %NULL, CURDIR$, %SW_SHOWNORMAL
                  It might also be done by shellexecute "outlook.exe /paramenters" (if command line options were known)
                  https://duckduckgo.com instead of google

                  Comment


                  • #10
                    To attach a file from the command line, you can try to use use the ATTACH syntax.
                    It doesn't work with all email clients though.

                    Code:
                    mailto:me@example.com?Subject=Hi&Body=See+attached+file&Attach="C:\readme.pdf" 
                    I've also read that
                    Code:
                    &Attachment="C:\readme.pdf"
                    

                    may work

                    Comment


                    • #11
                      Attachments are not part of the official mailto: URI specification (from which mail clients "borrow" their support), therefore if and how adding an attachment via command line is left to interpretation.

                      In such cases, I make both adding an attachment at all and the necessary parameter to do so configurable, so that a customer/user may be able to customize according to his mail client's supported/accepted parameters.

                      Comment


                      • #12
                        There are outlook specific command line arguments you could shell to.
                        Code:
                        outlook.exe /a "c:\test\14247.pdf" /m "test@test.com?subject=this is the subject&body=this is the body text"


                        The trick is knowing where outlook.exe is, per version.

                        Outlook command line switches:
                        https://www.howto-outlook.com/howto/...neswitches.htm

                        Comment


                        • #13
                          Originally posted by Shawn Anderson View Post
                          The trick is knowing where outlook.exe is, per version.
                          Office makes Outlook the default handler for *.msg files, so a modification to this code I posted recently: https://forum.powerbasic.com/forum/u...328#post779328 would do the trick i.e.:
                          Code:
                          #COMPILE EXE
                          #DIM ALL
                          #INCLUDE "win32api.inc"
                          
                          FUNCTION GetOutlook() AS STRING
                          ' Get Registration Key for default OPEN application for .Msg files (hopefully Outlook.exe)
                          LOCAL retval AS LONG
                          LOCAL hkey AS LONG
                          LOCAL opendialog AS ASCIIZ * 256
                          LOCAL extension AS ASCIIZ * 256
                          LOCAL READERCMD AS ASCIIZ * 256
                          Extension = ".msg"
                          retval = RegOpenKeyEx(%HKEY_CLASSES_ROOT,Extension,0,%key_query_value,hkey)
                          retval = regqueryvalueex(hkey,"",0,%reg_sz,opendialog,256)
                          retval = regclosekey(hkey)
                          extension = OPENDIALOG & "\SHELL\OPEN\command"
                          retval = RegOpenKeyEx(%HKEY_CLASSES_ROOT,extension,0,%key_query_value,hkey)
                          retval = regqueryvalueex(hkey,"",0,%reg_sz,READERCMD,256)
                          retval = regclosekey(hkey)
                          REPLACE " /f ""%1""" WITH "" IN readercmd
                          IF INSTR(UCASE$(readercmd),"OUTLOOK") = 0 THEN
                              readercmd = "Outlook is not the default opener for .msg"
                          END IF
                          FUNCTION = readercmd
                          END FUNCTION
                          
                          
                          FUNCTION PBMAIN () AS LONG
                          MSGBOX GetOutlook()
                          END FUNCTION

                          Comment


                          • #14
                            That's actually perfect! At first I thought that I'd have to find a .msg file, but all I had to do is create a file with a .msg extension and your program works perfectly. Thanks!

                            Comment


                            • #15
                              Originally posted by Shawn Anderson View Post
                              That's actually perfect! At first I thought that I'd have to find a .msg file, but all I had to do is create a file with a .msg extension and your program works perfectly. Thanks!
                              You don't need a .msg file to exist at all.

                              Comment

                              Working...
                              X