简体   繁体   English

用vba打开另一个包含所有宏的工作簿

[英]Open another workbook with vba that contains all the macros

Instead of having all the macro's stored in each workbook, we would like to have them stored in one global one. 与其将所有宏存储在每个工作簿中,不如将它们存储在一个全局的工作簿中。 We tried using Personal.xlsb file, however every time excel crashes or system administrator forced restart with excel open it created personal.v.01 ....v.100 files, and they interfered with each other, got corrupted etc.. So instead we are trying to add a small macro to each excel workbook we make which then should open a global excel workbook with all the macros, however it does not open it(normal.xlsb), where is the problem? 我们尝试使用Personal.xlsb文件,但是每次excel崩溃或系统管理员在excel打开时强制重新启动时,它都会创建personal.v.01 .... v.100文件,并且它们相互干扰,损坏等。因此相反,我们正在尝试向制作的每个Excel工作簿中添加一个小宏,然后应使用所有宏打开一个全局excel工作簿,但是它没有打开它(normal.xlsb),问题出在哪里? If I manually run it it works fine, it just does not autorun.. 如果我手动运行它,则工作正常,只是无法自动运行。

Option Explicit
Public Sub Workbook_Open()
Dim sFullName As String
Dim xlApp As Excel.Application
Dim wbReturn As Workbook
sFullName = "Z:\Dokumentstyring\normal.xlsb"
Set xlApp = GetObject(, "Excel.Application") 'need to do so to open it in same instance otherwise the global macros can not be called.
Set wbReturn = xlApp.Workbooks.Open(filename:=sFullName, ReadOnly:=True)
If wbReturn Is Nothing Then
MsgBox "Failed to open workbook, maybe z drive is down?"
Else
ThisWorkbook.Activate'Dont know how to pass object to modules, so instead activate it and in createbutton set wb= activeworkbook..
Application.Run ("normal.xlsb!CreateButtons")
End If
End Sub
Public Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wb As Workbook
For Each wb In Application.Workbooks
If InStr(UCase(wb.Name), "PARTSLIST") > 0 And wb.Name <> ThisWorkbook.Name Then Exit Sub
Next wb
On Error Resume Next
Workbooks("normal.xlsb").Close
Workbooks("filter.xlsx").Close
End Sub

You create your addin, as just an empty workbook, holding nothing but the code 您创建加载项,就像一个空的工作簿,只保留代码

Like this 像这样

在此处输入图片说明

Then you add a reference to it, in the workbook that you wish to use, in VBA, like this. 然后,在希望使用的工作簿中,像这样在VBA中添加对它的引用。 My Documents, is a folder on a network drive, not "my documents" local. 我的文档是网络驱动器上的文件夹,而不是本地的“我的文档”。

在此处输入图片说明

And then you can call like so. 然后您可以像这样打电话。

在此处输入图片说明

So based on input from @Nathan_Sav and @Ralph I have come to a partly good solution: 因此,根据来自@Nathan_Sav和@Ralph的输入,我得出了一个很好的解决方案:

I have called my addinn Normal and saved this on Z:Dokumenstyring\\Normal.xlam 我已将我的addinn称为Normal,并将其保存在Z:Dokumenstyring \\ Normal.xlam

I then removed all the code in Thisworkbook of Normal: 然后,我删除了Thisworkbook of Normal中的所有代码:

Private Sub Workbook_Open()

    Dim ExcelArgs As String
    Dim arg As String
    ExcelArgs = Environ("ExcelArgs")

    'Call deleteMacros.deletePersonalFiles
    'MsgBox ExcelArgs
    If InStr(UCase(ExcelArgs), "CREO,") > 0 Then
        Application.DisplayAlerts = False
        If Len(ExcelArgs) > Len("CREO,") Then
            arg = Split(ExcelArgs, ",")(1)
            Call Creo.addNewPartToPartslist(arg)
        End If
        Application.DisplayAlerts = True
    End If

    If InStr(UCase(ExcelArgs), "DOKLIST,") > 0 Then
        Application.DisplayAlerts = False
        If Len(ExcelArgs) > Len("DOKLIST,") Then
            arg = Split(ExcelArgs, ",")(1)
            Call ProsjektListen.dbDumpDocOut(arg)
        End If
        Application.DisplayAlerts = True
    End If

and put this in a new workbook called Z:Dokumenstyring\\Creo.xlsm 并将其放在名为Z:Dokumenstyring \\ Creo.xlsm的新工作簿中

I have so edited all my bat files(which previously were using personal.xlsb): 我已经编辑了所有的bat文件(以前使用personal.xlsb):

echo "Launch excel"
Set ExcelArgs=CREO,ADDPART

"C:\Program Files (x86)\Microsoft Office\OFFICE16\Excel.exe" /x /r "z:\Dokumentstyring\Creo.xlsm"

So when I run the bat file it adds a parameter to enviroment, start creo.xlsm, which then starts the addin which launch my userform. 因此,当我运行bat文件时,它向环境添加了一个参数,启动creo.xlsm,然后启动addin来启动我的用户窗体。

So if I now want to update the look of that that userform I do this by modifying the Z:Dokumenstyring\\NormalDebug.xlam, then i save a copy which i write over Z:Dokumenstyring\\Normal.xlam and I also told every user to not copy the addin to the default location in excel but keep it in Z:Dokumenstyring\\Normal.xlam. 因此,如果现在我想通过修改Z:Dokumenstyring \\ NormalDebug.xlam来更新该用户窗体的外观,那么我将保存一份副本,并将其写在Z:Dokumenstyring \\ Normal.xlam上,并且我还告诉每个用户不要将插件复制到excel中的默认位置,而是将其保存在Z:Dokumenstyring \\ Normal.xlam中。

My shapes in my excel sheets seems to work with just the macro name in the procedure, however there might be an conflict if two procedures have the same name, but located in different procedures. 我的excel工作表中的形状似乎仅与过程中的宏名称一起使用,但是,如果两个过程具有相同的名称,但位于不同的过程中,则可能会发生冲突。 So I also altered this to 所以我也将其更改为

ws1.Shapes(tempName).OnAction = "Normal.xlam!Custom_Button_Click" 

However every click starts a new instance of the addin, how to avoid this? 但是,每次单击都会启动该插件的新实例,如何避免这种情况? 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM