0

This is not a duplicate of: Can I set a Excel Application Object to point to an already open instance of Excel?

The idea is to execute a VBA sub contained in an Excel instance that is already open from Outlook

I am running the VBA sub as part of a rule in Outlook.

This is my code:

On Error Resume Next
        Dim tPath As String
        tPath = "X:\Lucas\LucasSheet.xlsm"
        Dim exApp As New Excel.Application
        Dim wb As Excel.Workbook
        wb = System.Runtime.InteropServices.Marshal.BindToMoniker(tPath)

Unfortunately at this point, when running in debug mode I can see that wb is equal to Nothing

        Set exApp = wb.Parent
        usedSub = "PrintSingle"
        exApp.Run usedSub            
        wb.Close False

Is it possible to make this code work in Outlook 2010?

Community
  • 1
  • 1
LucasSeveryn
  • 5,984
  • 8
  • 38
  • 65

3 Answers3

0

Instead of creating a new Excel Application in the code:

 Dim exApp As New Excel.Application

You need to get the running Excel instance :

exApp =  System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

See Access running instances of Excel in VB for more information and sample code in VB.NET.

Use the Run method of the Application class to run the VBA macro programmatically.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

First you need a function that will attach to a running instance of Excel and then look for a workbook by name.

'@Description "Return the open Excel workbook"
Public Function GetHandleForExistingWorkbook(ByVal fullPath As String) As Object
    On Error GoTo openExcel
    
    Dim excelApp As Object
    Set excelApp = GetObject(, "Excel.Application")
    
    Dim wb As Object
    For Each wb In excelApp.Workbooks
        If wb.FullName Like fullPath & "*" Then
            Set GetHandleForExistingWorkbook = wb
            Exit For
        End If
    Next wb

    Exit Function
    
openExcel:
    If Err.Number = 429 Then
        ' Open it if it wasn't already open
        Set excelApp = CreateObject("Excel.Application")
        GetHandleForExistingWorkbook = excelApp.Workbooks.Open(fullPath)
    Else
        Debug.Print "Unhandled exception: " & Err.Number & " " & Err.Description
    End If

End Function

Once you know you are in the correct place you can run the macro by calling on the Excel Application object:

Public Sub RunMacroInOpenWorkbook(ByVal fullPath As String, ByVal macroName As String, _
Optional ByVal macroParameters As String = "")

    Dim theWorkBook As Object
    Set theWorkBook = GetHandleForExistingWorkbook(fullPath)
    theWorkBook.Application.Run "'" & theWorkBook.Name & "'!" & macroName, macroParameters
    theWorkBook.Close False
End Sub

Then your code to use this would look like this:


    Dim tPath As String
    tPath = "X:\Lucas\LucasSheet.xlsm"
    usedSub = "PrintSingle"
    RunMacroInOpenWorkbook tPath, usedSub   

HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • Note that `GetObject(, "Excel.Application")` will return some instance of Excel and this might not be what you want if there are multiple instances running. [Here](https://stackoverflow.com/a/35343847/9700298) is code which gets _all_ instances. – Traveler Sep 13 '22 at 04:59
-1

You need to include the workbook name when using the Application.Run command.

Try using this:

    exApp.Run wb.Name & "!" & usedSub ' must include workbook name
ChipsLetten
  • 2,923
  • 1
  • 11
  • 28
  • you are opening a sheet instead of referring to one that is already open, that's not the point of my question. – LucasSeveryn Jun 18 '15 at 09:22
  • this is irrelevant to the question as `wb` is equal to`Nothing`. Your `wb.Name` will fail in my question. – LucasSeveryn Jun 18 '15 at 09:34
  • Have you tried using an `Auto_Open` macro in the workbook? Also, your question needs the [.Net] tag and more explaining. "How can i loop through the multiple open Excel sessions, looking for a workbook and setting a reference to that workbook?" – ChipsLetten Jun 18 '15 at 10:43
  • I want to run a macro on a sheet that's constantly open from Outlook, what's the reasoning behind adding anything to auto open? – LucasSeveryn Jun 18 '15 at 10:55