shapes.onaction in add-in using reference path to macro (Excel VBA)

MrIfOnly

Active Member
Joined
Nov 10, 2016
Messages
497
Office365
Windows 11

I have a VBA program running as an Excel Add-in which deposits multiple shapes onto the user's worksheet. These shapes (for example, the two shown in the picture below labeled "Quality Check" and Undo Sort) use the .onaction property to run a macro that is located in a standard module in the add-in. Absolutely no code is stored in the user's workbook.

The shapes code is this:
VBA Code:
    If shapeexists("QLTYCHK") = False Then
        With ActiveSheet.Shapes.ADDSHAPE(msoShapeRoundedRectangle, Range("K33").Left, Range("K33").Top, 150, 60)
            .Name = "QLTYCHK"
            .OnAction = "QualityCheck"
            .Fill.ForeColor.RGB = RGB(305, 415, 0)
            .Fill.BackColor.RGB = RGB(155, 285, 0)
            .Fill.TwoColorGradient msoGradientHorizontal, 1
            .Fill.Transparency = 0
            With .TextFrame
                .MarginBottom = 3
                .MarginLeft = 10
                .MarginRight = 10
                .MarginTop = 3
                .HorizontalAlignment = xlCenter
                .Characters.Font.Color = vbBlue
                .Characters.Font.Size = 19
                .Characters.Font.Bold = True
                .Characters.Text = "QUALITY CHECK"
            End With
        End With
    End If
This works just fine.
The problem arose from a user sending me a sheet with these shapes which work for him, but when I click on them I get an error message that reads "Sorry, Excel can't open two workbooks with the same name at the same time." followed by another message that reads: "Cannot run the macro "C:\Users\thchi\AppData\Roaming\Microsoft\AddIns\NozzleScheduleAutomation.xlam'!QualityCheck'. The macro may not be available in this workbook or all macros may be disabled."
The issue is that, when the onaction property is set to the shape, it is using the absolute path of the Add-in on the user's PC. So I tried to make this a relative path by trying:
VBA Code:
.OnAction = "'%APPDATA%\Roaming\Microsoft\AddIns\NozzleScheduleAutomation.xlam'!QualityCheck"
and even trying to assign "Environ('APPDATA')" to a string variable and tacking it onto the .onaction code, to no avail.
Screenshot 2024-11-19 231221.png


You may have noticed that the above worksheet is opened from an Autocad drawing as an OLE, however my problem is repeatable on plain worksheets not attached to anything. Also, in order to test any variation to the code, I have to create the worksheet with the shapes on one PC and try to run them on a different PC with a different username.

If anybody has any ideas about how to make this work so that the program will run regardless of the PC it is run from, I would be eternally grateful.

TIA,

CJ
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Is there a reason for putting shapes on the worksheet rather than using a custom ribbon tab/group created by the add-in? It would avoid this issue.
 
Upvote 0
You could add an application event class that responds to opening of workbooks, which then goes through the shapes of the new workbook and updates their OnAction setting to the current add-in incarnation. IIRC you could use the workbook links for this
 
Upvote 0
Is there a reason for putting shapes on the worksheet rather than using a custom ribbon tab/group created by the add-in? It would avoid this issue.
Thanks for the suggestion, Rory. It would avoid the issue, however, I have 9 icons on a custom ribbon tab already and I used the shapes to present certain options if only certain conditions are met. Once the shapes run their macros, they are removed and the are reintroduced only if those conditions are met again. I might consider adding more icons to the ribbon if I could disable/enable them as needed via VBA. I have not looked into that possibility yet.
 
Upvote 0
You could add an application event class that responds to opening of workbooks, which then goes through the shapes of the new workbook and updates their OnAction setting to the current add-in incarnation. IIRC you could use the workbook links for this
This might be the best option. I will try something along these lines tomorrow when my eyes can focus on the monitors again. Thanks Jan!
 
Upvote 0
You can show/hide or enable/disable controls on the ribbon using callbacks.
 
Upvote 1
Thank you Rory and Jan for the help. After doing some research into callbacks (this page written by John at The VBA Help was especially helpful) I am certain that you have pointed me down the road I need to follow.
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top