如何将此代码实现到Excel加载项中?

时间:2016-02-23 16:52:01

标签: excel vba excel-vba add-in

我有一个大的Excel宏,我变成了一个加载项(.xlam)文件,因为我将在共享网络驱动器上保留此加载项的公共版本,我遵循了以下建议:来自here的Ken Puls,通过使用以下代码来部署我的加载项的未来版本(用于更新,修复等)。:

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

现在,我了解代码中发生了什么,我只是不确定应该放置Sub的位置。它应该放在.xlam的ThisWorkbook Module,Module1(包含我的宏)还是其他地方?我很困惑,因为安装了加载项的用户不能访问/运行此Sub吗?我有加载项本身被锁定,不知道这是否有帮助,我有一个按钮放在加载项工具栏区域,用户可以单击以运行我的宏。

1 个答案:

答案 0 :(得分:0)

与网站所有者交谈后,现在可以清楚地了解如何实现上述代码。此Sub放置在加载项内的模块中。如果您不希望最终用户具有访问/能够运行此Sub的权限,请将其设置为 Private Sub(这会清除我的第一个问题)。要运行Sub,您必须在vbe中并在Sub中单击光标,然后单击Run按钮。

相关问题