Results 1 to 2 of 2

Thread: Outlook + Rule + VB Script: How to Enable?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Location
    Indiana, USA
    Posts
    2

    Outlook + Rule + VB Script: How to Enable?

    Hello. I'm new to the forums.

    Q: Assuming I have good and functioning script and I've set up a rule that calls that script, is there anything else I need to do to get VB scripts to run from rules in Outlook 2007? Is there some setting I need to set in order to get scripts to run from rules?

    Possibly useful background info:

    Our company uses MS Exchange on a server and I am using the Outlook client. Our office telephone system (Avaya IP Office) routes voicemails as emails to our Outlook email accounts. I've been trying to have the huge WAV attachments be saved to a folder automatically, so I figured a rule that called a script to save off the attachment would do the trick.

    I have found numerous posts all over the internet offering solutions, which from followup comments indicated they work well. Not only that, getting it to work never seemed to be an issue for others. However, when I copy and paste the code into Outlook's VB editor, set up a rule to call that script, it won't run the script. I'll post some samples of the scripts at the end.

    As far as the rules, I've set up numerous rules and they work fine. For the voicemail emails, I actually have two rules with identical criteria but the first calls the script. The second moves it to the Deleted Items folder. I'm pretty confident it's not an issue with the rule setup.

    Once I was editing a macro and somehow managed to get the script to be recognized (it ran when I tested it on a message in my inbox). But the next day, it didn't work.

    I thought it might be a security issue, so some searching led to me try creating a digital certificate via 'Digital Certificate for VBA Projects'. I attached the certificate to my vbproject and saved, but that didn't work.

    I did do a search and also checked the FAQ/Index. I read through the posts but didn't find this specific question in there.

    Hoping you can help. Thanks in advance.

    [Script 1]
    Code:
    Sub SaveToFolder(MyMail As MailItem)
        Dim strID As String
        Dim objNS As Outlook.NameSpace
        Dim objMail As Outlook.MailItem
        Dim objAtt As Outlook.Attachment
        Dim c As Integer
        Dim save_name As String
        
        MsgBox "Started Script to Save Attachment"
        
        'Place path to sav to on next line. Note that you must include the final backslash
        Const save_path As String = "H:\Voicemails\"
        
        strID = MyMail.EntryID
        Set objNS = Application.GetNamespace("MAPI")
        Set objMail = objNS.GetItemFromID(strID)
        
        If objMail.Attachments.Count > 0 Then
            For c = 1 To objMail.Attachments.Count
                Set objAtt = objMail.Attachments(c)
                save_name = Left(objAtt.FileName, Len(objAtt.FileName) - 4)
                save_name = Format(objMail.ReceivedTime, "yyyy-mm-dd_hh-mm_") & save_name
                save_name = save_name & Right(objAtt.FileName, 4)
                objAtt.SaveAsFile save_path & save_name
            Next
        End If
      
        Set objAtt = Nothing
        Set objMail = Nothing
        Set objNS = Nothing
    
    End Sub
    [Script 2]
    Code:
    Sub SaveAttachments(Item As Outlook.MailItem)
        
        Dim iAttachCnt As Integer
        Dim MsgSndr As String
        Dim MsgSubj As String
        Dim MsgDate As String
        Dim SvFileNm As String
        Dim tempFN1 As String
        Dim tempFN2 As String
        Dim tempInt As Integer
        Dim newFN As String
        Dim OrigFN As String
        Dim i As Integer
        Dim SAVEPATH As String
        
    '   Manually Set Path
        SAVEPATH = "H:\Voicemails\"
    
    '   Get count of attachments
        iAttachCnt = Item.Attachments.Count
    
        MsgSubj = Item.Subject
        MsgDate = Item.ReceivedTime
        MsgSndr = Right(MsgSubj, Len(MsgSubj) - InStrRev(MsgSubj, ":"))
        
        If iAttachCnt > 0 Then
            For i = 1 To iAttachCnt
                OrigFN = Item.Attachments(i).FileName
                SvFileNm = SAVEPATH & "Tel_" & MsgSndr & "_" & OrigFN
                MsgBox "Voicemail Saved: " & SvFileNm
                Item.Attachments.Item(i).SaveAsFile SvFileNm
            Next i
        End If
    
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Outlook + Rule + VB Script: How to Enable?

    i don't know about calling scripts from rules, but
    i believe that outlook has a mail arrival event, you could check the criteria there then call the script
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width