Search code examples
vbaribbon

VBA Ribbon Bar Integration


I have an Excel macro that performs a few functions on a document (Creates as form, and a few emails) all from MS-Word documents. If the macro is executed from the main spreadsheet (where the macro is), everything works normally. I want to place this macro on the ribbon allowing a user to launch it without having (or knowing where the main excel document is located or having it open). I created a sub to check to see if the spreadsheet was open and modified the ribbon to include an icon for the macro.

Which works. However, when launched from the ribbon while the main Excel spreadsheet is not open, it opens the workbook and runs the macro in entirety (Without executing the open workbook line of the macro). I assume the spreadsheet is being open because the macro that is being called resides with it (makes sense). Since the macro is dependent on the data contains in the spreadsheet, I need to allow the users to modify it and then re-running the macros from the ribbon again. Does anyone have a recommended approach or best practices? Thank you in advance.

Sub MainForm()
    Dim WorkingFolder As String
    Dim File01 As String                               'Main Excel Data File, where all data is
    Dim File02 As String                               'Preliminary Email to send to user
    Dim File03 As String                               'Final Email to Send to user when production is complete
    Dim wb As Workbook
    
    WorkingFolder = "C:\Temp\"
    File01 = "01-MainData.xlsm"
    File02 = "02-PreProductionEmail.docx"
    File03 = "03-FinalProductionEmail.docx"
    
    If wbIsOpen(File01) = True Then
       MsgBox "Workbook Is Open"
       Run ("'C:\Users\Guest\Nextcloud\Documents\Excel Forms\02-TEST-Production Request-Data.xlsm'!CreateProductionForm")   
    Else
        MsgBox "The Main Datafile is not open, verify the last row before re-runing", vbOKOnly, "Not Open"
        Set wk = Workbooks.Open(WorkingFolder & DataFile)
    End If
End Sub

Solution

  • The Guide mentioned by Ricardo was not quite what I needed but it was helpful and did put me on the right track. Thank you again Ricardo. To get this working, I needed to do the following.

    1 - Make the procedure above its own separate *.xlam file (saved as an add-in).

    2 - Add the add-in to start automatically via the developers tab

    3 - Add the Macro to the Ribbon.

    I have modified the sample above to included the open statement along with the statement that executes a macros from another workbook.

    Appreciate the guidance.