How would I implement this code into my Excel Add-In?

£可爱£侵袭症+ 提交于 2019-12-12 02:26:10

问题


I have a large Excel macro that I turned into an Add-In (.xlam) file and since I will be keeping the public version of this Add-In on a shared network drive I was following the advice of Ken Puls from here, by using the following code to be able to deploy future versions of my Add-In (for updates, fixes, etc).:

Sub DeployAddIn()
'Author       : 
'Macro Purpose: To deploy finished/updated add-in to a network
'               location as a read only file
    Dim strAddinDevelopmentPath As String
    Dim strAddinPublicPath As String

    'Set development and public paths
    strAddinDevelopmentPath = ThisWorkbook.Path & Application.PathSeparator
    strAddinPublicPath  = "F:\Addins" & Application.PathSeparator

    'Turn off alert regarding overwriting existing files
    Application.DisplayAlerts = False

    'Save the add-in
    With ThisWorkbook
        'Save to ensure work is okay in case of a crash
        .Save

        'Save read only copy to the network (remove read only property
        'save the file and reapply the read only status)
        On Error Resume Next
        SetAttr strAddinPublicPath & .Name, vbNormal
        On Error Goto 0
        .SaveCopyAs Filename:=strAddinPublicPath  & .Name
        SetAttr strAddinPublicPath & .Name, vbReadOnly
    End With

    'Resume alerts
    Application.DisplayAlerts = True
End Sub

Now, I understand what's going on in the code, I'm just not sure where this Sub should be placed. Should it be placed in the .xlam's ThisWorkbook Module, Module1 (which houses my macro), or somewhere else? I'm confused because wouldn't the users that have the Add-In installed be able to access/run this Sub? I have the Add-In itself locked, not sure if that helps at all, and I have a button that gets placed on the Add-Ins toolbar area that the users can click to run my macro.


回答1:


After talking with the site owner it is now clear on how to implement the above code. This Sub is placed in a Module inside the Add-In. If you don't want end users to have access/ability to run this Sub, then set it as a Private Sub (this clears up my first question). To run the Sub you have to be inside the vbe and have the cursor clicked inside the Sub, then click the Run button.



来源:https://stackoverflow.com/questions/35583430/how-would-i-implement-this-code-into-my-excel-add-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!