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

时间:2016-11-15 09:11:08

标签: excel vba excel-vba macros

我们希望将它们存储在一个全局工作簿中,而不是将所有宏存储在每个工作簿中。我们尝试使用Personal.xlsb文件,但每次excel崩溃或系统管理员强行重启excel打开它创建了personal.v.01 .... v.100文件,他们互相干扰,损坏等等。所以相反,我们正在尝试为我们制作的每个excel工作簿添加一个小宏,然后应该打开一个包含所有宏的全局excel工作簿,但是它不会打开它(normal.xlsb),问题出在哪里?如果我手动运行它工作正常,它只是不自动运行..

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

2 个答案:

答案 0 :(得分:1)

你创建你的插件,只是一个空的工作簿,只保留代码

喜欢这个

enter image description here

然后在VBA中添加对您要使用的工作簿中的引用,就像这样。我的文档,是网络驱动器上的文件夹,而不是&#34;我的文档&#34;本地

enter image description here

然后你可以这样打电话。

enter image description here

答案 1 :(得分:0)

所以基于来自@Nathan_Sav和@Ralph的输入,我得到了一个很好的解决方案:

我已将我的addinn Normal调用并将其保存在Z:Dokumenstyring \ Normal.xlam

然后我删除了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

并将其放入名为Z:Dokumenstyring \ Creo.xlsm

的新工作簿中

我编辑了所有我的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"

因此,当我运行bat文件时,它会向enviroment添加一个参数,启动creo.xlsm,然后启动addin以启动我的userform。

所以,如果我现在想通过修改Z:Dokumenstyring \ NormalDebug.xlam来更新那个用户形态的外观,那么我保存一份我在Z上写的副本:Dokumenstyring \ Normal.xlam我也告诉过每个用户都不要将插件复制到excel中的默认位置,而是将其保存在Z:Dokumenstyring \ Normal.xlam。

我的excel表单中的形状似乎只与过程中的宏名称一起使用,但是如果两个过程具有相同的名称但位于不同的过程中,则可能存在冲突。所以我也将其改为

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

但是每次点击都会启动一个新的插件实例,如何避免这种情况?  enter image description here

相关问题