skatun
skatun

Reputation: 877

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? 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

Upvotes: 0

Views: 983

Answers (2)

skatun
skatun

Reputation: 877

So based on input from @Nathan_Sav and @Ralph I have come to a partly good solution:

I have called my addinn Normal and saved this on Z:Dokumenstyring\Normal.xlam

I then removed all the code in 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

I have so edited all my bat files(which previously were using 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.

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.

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. 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? enter image description here

Upvotes: 0

Nathan_Sav
Nathan_Sav

Reputation: 8531

You create your addin, as just an empty workbook, holding nothing but the code

Like this

enter image description here

Then you add a reference to it, in the workbook that you wish to use, in VBA, like this. My Documents, is a folder on a network drive, not "my documents" local.

enter image description here

And then you can call like so.

enter image description here

Upvotes: 1

Related Questions