VBA/AppleScript

Discussion in 'Mac Programming' started by chibeardia, May 18, 2017.

  1. chibeardia macrumors newbie

    Joined:
    May 18, 2017
    #1
    I have this successfully working in Outlook proper. I would like to convert it to apple script.


    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)


    If TypeOf Item Is Outlook.MailItem Then

    Dim olMsg As MailItem

    Set olMsg = Item

    Dim Prompt As String


    Prompt = "***WARNING***" & vbNewLine & vbNewLine & _

    "Do you want to CC this message to AVH?"


    If MsgBox(Prompt, vbYesNo + vbQuestion, "CC AVH?") = vbYes Then

    Set myRecipient = olMsg.Recipients.Add("Van Holsbeke, Ashley")

    myRecipient.Type = olCC

    myRecipient.Resolve

    End If



    Set olMsg = Nothing



    End If


    End Sub
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    It might be useful for others trying to help you to include the actual result of the VBA code. It looks like it does something to a mail message, pops up a message box, adds a name to recipients. Right? A good starting point would be to look at Outlook"s dictionary.
     
  3. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #3
    ...or even better, explain what the above code does. I can hazard a guess, but there won't be a huge number of VBA experts on here. Also, let us know what you've tried so far.
     
  4. chibeardia thread starter macrumors newbie

    Joined:
    May 18, 2017
    #4
    apologies...The purpose is to have a pop up to ask if the user would like to cc a specific address



    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Call outlook


    If TypeOf Item Is Outlook.MailItem Then

    Dim olMsg As MailItem

    Set olMsg = Item

    Dim Prompt As String


    Prompt = "***WARNING***" & vbNewLine & vbNewLine & _

    "Do you want to CC this message to AVH?" popup that asks if you want to cc a specific user


    If MsgBox(Prompt, vbYesNo + vbQuestion, "CC AVH?") = vbYes Then if yes is click add the below address to the email

    Set myRecipient = olMsg.Recipients.Add("Van Holsbeke, Ashley")

    myRecipient.Type = olCC

    myRecipient.Resolve this calls for a resolve of the above user to the correct email/contact

    End If



    Set olMsg = Nothing



    End If


    End Sub
     
  5. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #5
    Hmmm. Am I right in thinking you're wanting to pop this up when a user tries to send an email? If so, I'm not sure if you can intercept an "mail send" event with AppleScript as appears to be possible with VBA.

    Unless anyone else here knows differently?
     
  6. chibeardia thread starter macrumors newbie

    Joined:
    May 18, 2017
    #6
    Here is the popup from outlook.
     

    Attached Files:

  7. Mark FX, May 19, 2017
    Last edited: May 19, 2017

    Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #7
    Although I haven't done any VB coding for about 15 years, or indeed used Microsoft products for as long as well.

    I think your misunderstanding the difference between VBA and AppleScript.
    Although your VBA code can be interpreted into AppleScript code, this would not help you.

    VBA code is used as an application extension or plugin to the host application, and uses an OLE automation mechanism, and is driven by Application Events, as opposed to AppleScript's system wide Apple Event mechanism.

    Where as your VBA code module is almost part of the application's features, which could be run from the applications menu or toolbar, any AppleScript file would be an external script file, which would have to be double clicked or activated before it could perform any actions.

    But I believe if memory serves me correct, you can create a VBA code module from within the Outlook application with this sort of code here.

    Code:
    #VB Code
    Dim myScriptResult as String
    myScriptResult = AppleScriptTask ("MyAppleScriptFile.scpt", "myapplescripthandler", "my, parameter, string")
    
    And then create an AppleScript with Script Editor and save it in ~/Library/Application Scripts/com.microsoft.outlook/ folder, with the above name MyAppleScriptFile.scpt.

    Because I've never had Outlook installed on any Mac, I don't have access to Outlook's AppleScript Dictionary, so I can't help you with specific Outlook Scripting, but the base of the AppleScript code using the above VBA code would be something like this.

    Code:
    -- AppleScript Code
    on myapplescripthandler(param1, param2, param3)
        -- Do stuff with the handler params here
        tell application id "com.microsoft.outlook"
            -- You can also do stuff with handler params here as well
            -- Do stuff with the Outlook Application here
        end tell
        -- Because your VBA code module is expecting a String result, tell it what happened here
        return theResult as string
    end myapplescripthandler
    
    If you need any further help with AppleScript language specific things, I'll be happy to help.
    But I can't test any Outlook application specific stuff, without access to the app or it's AppleScript dictionary.

    I think you'll probably find loads of Outlook scripting resources online with a bit of searching.

    Hope this helps

    Regards Mark
     
  8. chibeardia thread starter macrumors newbie

    Joined:
    May 18, 2017
    #8
    I more than likely am misunderstanding it :confused::eek:...lol. I appreciate the insight. I will keep you posted with the results.

    I appreciate the help Mark.



     
  9. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #9
    At this point I'm questioning your reasons for wanting to use AppleScript for Microsoft Office automation.
    Microsoft introduced Visual Basic for Applications a long time ago, with the main purpose of using it for automation of tssks within Office applications.
    Clearly you know how to access the developer tools within the Outlook app, and you seem to be comfortable with the Visual Basic programming language.
    So I feel compelled to ask the obvious question, why don't you use the built in automation tools?

    The VB code modules work within the application's sandbox, and can use the application's native data types, while also being accessible from the application's menu's and toolbar's.
    Surley that's preferable to an external automation script, written in a language your not familiar with.
    Don't get me wrong, I'm sure you have good reasons, but I'm struggling to work them out myself.

    Ironically the Apple industry experts, of which I am NOT one, have been rumouring over the last couple of years about AppleScript possably being deprecated, in favour of application extensions written in something like JavaScript.
    I'm not sure how much we can trust these rumours, but this type of app extension could well end up being something similar to Microsoft's VBA system.
    Ultimately only Apple insiders no the future of scripting and automation on Apple's platforms.

    Regards Mark
     
  10. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #10
    Yeah, I've been hearing that one for over ten years now! I think the problem is that they'd have to replace it with something as good or better (which I'd be over the moon about!).
     
  11. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #11
    Yeah I think we've had this conversation before.

    Like you I've also heard it a lot over the years, and will only beleive it when it happens.
    But the warning signs are definatley there this time, with the tighter sanboxing rules, the JavaScript tab in the Xcode Documentation window, and the Cocoa Frameworks rapid shift towards Swift.
    And also the departure of Sal Soghoian recently from Apple's employ, suggest changes in Apple's automation technologies may be closer than ever.

    But as we've also both said before, it's a case of wait and see.

    Mark
     
  12. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #12
    Hah hah! I thought I had a sense of deja vu there. Sorry - one of my pet subjects... ;-)
     

Share This Page