Consulting

Results 1 to 14 of 14

Thread: from outlook, open excel, open workbook, run macro based on URL in body

  1. #1
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location

    from outlook, open excel, open workbook, run macro based on URL in body

    so what i need help with is in getting the URL from the outlook body to be the parameter to my excel macro. here is what i have so far.

    Sub runExcelMacro()Dim ExApp As Excel.Application
    Dim ExWbk As Workbook
    Set ExApp = New Excel.Application
    Set ExWbk = ExApp.workboooks.Open("W:\Viewpoint\Viewpoint Import\Programs\SheetsForNavigationAndImportingIntoViewpoint FROZEN.xlsm")
    ExApp.Visible = True
    
    
    ExWbk.Application.Run "Module1.startSaveModule"
    'need to read URL from body (there will be a keyword in the subject line - "extraction". there will only be the URL in the body, nothing else, no signatures as well.
    ExWbk.Close savechanges:=False
    
    
    End Sub
    thoughts

  2. #2
    If the only thing in the message body is the text you want to recover
    and
    Your macro in Excel includes a callback e.g. startSaveModule(strURL as String)
    Then the following should work.
    I have not included code to test whether the workbook is already open, nor to close it after completion.


    Option Explicit
    
    Sub Test()
    Dim olMsg As MailItem
        On Error Resume Next
        Set olMsg = ActiveExplorer.Selection.Item(1)
        runExcelMacro olMsg
    lbl_Exit:
        Exit Sub
    End Sub
    
    Sub runExcelMacro(olItem As Object)
    Dim ExApp As Object
    Dim ExWbk As Object
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim oPara As Object
    Dim strURL As String
    Const strWB As String = "W:\Viewpoint\Viewpoint Import\Programs\SheetsForNavigationAndImportingIntoViewpoint FROZEN.xlsm"
    
        If Not TypeName(olItem) = "MailItem" Then GoTo lbl_Exit
        If InStr(1, olItem.Subject, "extraction") = 0 Then GoTo lbl_Exit
    
        With olItem
            .BodyFormat = olFormatHTML
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range
            For Each oPara In oRng.Paragraphs
                If Len(oPara) > 1 Then
                    Set oRng = oPara.Range
                    oRng.End = oRng.End - 1
                    strURL = Trim(oRng.Text)
                    Exit For
                End If
            Next oPara
    Debug.Print strURL
        End With
        On Error Resume Next
        Set ExApp = GetObject(, "Excel.Application")
        If Err <> 0 Then
            Set ExApp = CreateObject("Excel.Application")
        End If
        On Error GoTo 0
        Set ExWbk = ExApp.workbooks.Open(strWB)
        ExApp.Visible = True
    
        ExApp.Run "Module1.startSaveModule", strURL
    
    lbl_Exit:
        Set ExApp = Nothing
        Set ExWbk = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    Thanks gmayor, i am implementing that now. nice piece of work. kudos. might have a question or two on implementation.

  4. #4
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    so a question on this line:

    ExApp.Run "Module1.startSaveModule", strURL
    it does not run the macro and i don't know why. i verified that the workbook opened up (it did) and then nothing else happened. what am i doing wrong.

    note: i got it to run successfully, so thanks for your code. just one more thing, how can i get the test() to run automatically, in other words, can it be memory resident and always scanning for this type of file?
    Last edited by danmc58; 01-29-2019 at 11:14 AM.

  5. #5
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    bump, can anyone help with this. just need to make it run automatically.

    thanks

  6. #6
    To run it automatically, associate it with an Outlook rule that runs when the messages arrive.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    im pretty comfortable with rules in outlook, but don't know how to associate the macro with the email. i can create a rule that moves a message to a folder based on keywords in the subject line, but get lost figuring out how to associate it with a macro. does that make sense?

  8. #8
    In the rule creation dialog create a rule that applies to messages as they arrive and the action is Run a script. The script would be runExcelMacro
    If there is no script option - see https://www.extendoffice.com/documen...t-missing.html
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    is there a reference that i need? when i go to outlook and create a rule, the action dialog box does not include "Run a Script". see attachment.

    rules wizard actions in outlook.jpg

  10. #10
    Did you miss the comment at the end of my previous post?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    yes i did miss that. sorry. so i went through the article and i don't have the Outlook key and of course no Security key. can you tell me the properties of both so i can create them? sorry if this is a pain.

  12. #12
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    so i found slipstick had a reg key for it and i got the script to run. now outlook can see running a script. but when i do that, nothing comes up in the macro dialog box, i made sure that my subs are in a ThisOutlookSession and that they can run manually. what am i doing wrong?
    Last edited by danmc58; 02-01-2019 at 07:56 AM.

  13. #13
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    how can i mark the message as read?

    note still have issue where rule script has nothing in the dialog box, how do i make the macro appear in that box?

    found article on how to make macro appear, so now when a new message appears with the right info, then it runs but does not complete. will get back to you once i have this figured out.
    Last edited by danmc58; 02-01-2019 at 08:23 AM.

  14. #14
    VBAX Regular
    Joined
    Apr 2016
    Posts
    22
    Location
    so got it to mark the item as read, run though the script, close excel. thanks for all your help.

Posting Permissions

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