T
thedude
I've gone through all the forum postings from across the internet and
have managed to piece this much together, but there are some issues
that are inconsistently popping up still...
What i'm attempting to create is an add-in for the department with
some menu items and a couple userforms. What i'd like it to do is
1: Delete the current 'add-in' in the XLStart folder
2: Check for previous add-ins
2a: if Previous add-ins exists, delete them
2b: if its the same version, just overwrite it.
2c: remove the previous add-ins from the Add-ins menu manager
3: Install new Add-in and check it in the Add-in menu
4: Run the code to set up the department menus
5: Delete the module that is doing the installation.
Issues that i'm having
1: None, this part works
2a: I can find the previous Add-ins and delete them
2b: This part works, but not consistently. Half the time it gives me
an error...
2c: Very rarely works, but does sometimes??? Normally not, though,
then you get the error when Excel restarts
3: Seems to do okay unless the Add-in was there already and just
overwritten. Then it's inconsistent.
4: It only temporarily installs them. The .xlb file does not update,
though, so after restarting - only the top level Menus remain, but
none of the MenuItems.
5: Works sometimes, but not very often? I can't see any pattern to
it. Also, when it does work, how do you save the updated VBA code?
Every time it has worked, when Excel restarts it's there again. I
can't find any functions other than .SaveAs, but i'm not sure how to
implement it as a i get an error (Object doesn't support this class)
when i try...
Any help would be greatly appreciated.
Thanks,
Thedude
______________________________________________
______________________________________________
Sub Autpen()
Delete_XLSTART_XLA
DeletePrevAddIn
InstallAddIn
RemoveItems
AddMenus
AddMenuItems
End Sub
______________________________________________
______________________________________________
Private Function Delete_XLSTART_XLA()
On Error Resume Next
Dim x As String, MyName As String
Dim i As Integer
Dim Response As Integer, TotalFiles As Integer
Dim FileArray As Variant
x = "C:\Documents and Settings\" & Application.UserName & _
"\Application Data\Microsoft\Excel\XLSTART"
With Application.FileSearch
.NewSearch
.FileType = msoFileTypeAllFiles
.LookIn = x
.Execute
If .FoundFiles.Count > 0 Then
ReDim FileArray(1 To .FoundFiles.Count)
For i = 1 To .FoundFiles.Count
If Right(.FoundFiles(i), 3) = "xla" Then _
FileArray(i) = Right(.FoundFiles(i), _
Len(.FoundFiles(i)) - Len(x) - 1)
Next i
If UBound(FileArray) > 0 Then
i = 1
For Each file In FileArray
If InStr(1, FileArray(i), "Mass", vbBinaryCompare) <>
0 Then _
Kill .FoundFiles(i)
i = i + 1
Next
End If
End If
End With
End Function
______________________________________________
______________________________________________
Private Function DeletePrevAddIn()
Dim FileArray As Variant
x = "C:\Documents and Settings\" & Application.UserName & _
"\Application Data\Microsoft\Addins"
With Application.FileSearch
.NewSearch
.FileType = msoFileTypeAllFiles
.LookIn = x
.Execute
If .FoundFiles.Count > 0 Then
ReDim FileArray(1 To .FoundFiles.Count)
For i = 1 To .FoundFiles.Count
If Right(.FoundFiles(i), 3) = "xla" Then _
FileArray(i) = Right(.FoundFiles(i), _
Len(.FoundFiles(i)) - Len(x) - 1)
Next i
End If
End With
With ThisWorkbook
If i > 1 Then
i = 1
For Each file In FileArray
If InStr(1, FileArray(i), "SearchString1",
vbBinaryCompare) <> 0 Then _
AddIns(Left(FileArray(i), Len(FileArray(i)) - 4)) _
.Installed = False
AddinTitle = Left(.Name, Len(.Name) - 4)
AddinName = AddinTitle & ".xla"
If AddinName <> FileArray(i) Then _
Kill x & "\" & FileArray(i)
i = i + 1
Next
End If
End With
End Function
______________________________________________
______________________________________________
Private Function InstallAddIn()
Dim AddinTitle As String, AddinName As String
Dim XlsVersion As String, MessageBody As String
Dim wksht As Workbook
Dim i As Long, j As Long
Dim wkshtnames() As Variant
i = 0
For Each wksht In Workbooks
i = i + 1
Next wksht
If i = 0 Then Application.Workbooks.Add
With ThisWorkbook
AddinTitle = Left(.Name, Len(.Name) - 4)
AddinName = AddinTitle & ".xla"
Application.DisplayAlerts = False
.SaveAs Application.UserLibraryPath & AddinName
Application.DisplayAlerts = True
AddIns.Add(ThisWorkbook.FullName, True) _
.Installed = True
End With
End Function
______________________________________________
______________________________________________
Private Function RemoveItems()
For Each ctl In Application.CommandBars("Worksheet Menu Bar").Controls
header_name = ctl.Caption
head_count = Len(header_name)
For i = 1 To Len(header_name)
If Right(Left(header_name, i), 1) = "&" Then
header_name = Left(header_name, i - 1) & _
Right(header_name, head_count - i)
End If
Next
find_item = "SearchString1"
find_item2 = "SearchString2"
If InStr(1, header_name, find_item, vbBinaryCompare) <> 0 _
Then ctl.Delete
If InStr(1, header_name, find_item2, vbBinaryCompare) <> 0 _
Then ctl.Delete
Next
End Function
______________________________________________
______________________________________________
Private Function AddMenus()
HelpMenu = Application.CommandBars("Worksheet Menu
Bar").Controls("Help").Index
Set cbMainMenuBar = _
Application.CommandBars("Worksheet Menu Bar")
Set cbMPE_Analysis = _
CommandBars("Worksheet Menu
Bar").Controls.Add(Type:=msoControlPopup, _
before:=HelpMenu)
Set cbMPE_KServer = _
cbMainMenuBar.Controls.Add(Type:=msoControlPopup, _
before:=HelpMenu + 1)
With cb_Analysis
.TooltipText = "Menu1"
.Caption = "Menu Item1"
End With
End Function
______________________________________________
______________________________________________
Private Sub AddMenuItems()
Set Menu_analysis = MenuBars(xlWorksheet).Menus("Menu1")
With Menu_analysis
.MenuItems.AddMenu Caption:="Menu Item1"
.MenuItems("Menu Item1a").MenuItems.Add _
Caption:="Menu Itema", _
onaction:="Menu Itema"
.MenuItems("Menu Item1b").MenuItems.Add _
Caption:="Menu Itemb", _
onaction:="Menu Itemb"
End With
End Sub
______________________________________________
______________________________________________
Sub Auto_Close()
Dim x As Object
Set x = Application.VBE.ActiveVBProject.VBComponents
x.Remove VBComponent:=x.Item("UpdateToolkit")
x.Save 'How do you get it to save after deleting modules??
End Sub
have managed to piece this much together, but there are some issues
that are inconsistently popping up still...
What i'm attempting to create is an add-in for the department with
some menu items and a couple userforms. What i'd like it to do is
1: Delete the current 'add-in' in the XLStart folder
2: Check for previous add-ins
2a: if Previous add-ins exists, delete them
2b: if its the same version, just overwrite it.
2c: remove the previous add-ins from the Add-ins menu manager
3: Install new Add-in and check it in the Add-in menu
4: Run the code to set up the department menus
5: Delete the module that is doing the installation.
Issues that i'm having
1: None, this part works
2a: I can find the previous Add-ins and delete them
2b: This part works, but not consistently. Half the time it gives me
an error...
2c: Very rarely works, but does sometimes??? Normally not, though,
then you get the error when Excel restarts
3: Seems to do okay unless the Add-in was there already and just
overwritten. Then it's inconsistent.
4: It only temporarily installs them. The .xlb file does not update,
though, so after restarting - only the top level Menus remain, but
none of the MenuItems.
5: Works sometimes, but not very often? I can't see any pattern to
it. Also, when it does work, how do you save the updated VBA code?
Every time it has worked, when Excel restarts it's there again. I
can't find any functions other than .SaveAs, but i'm not sure how to
implement it as a i get an error (Object doesn't support this class)
when i try...
Any help would be greatly appreciated.
Thanks,
Thedude
______________________________________________
______________________________________________
Sub Autpen()
Delete_XLSTART_XLA
DeletePrevAddIn
InstallAddIn
RemoveItems
AddMenus
AddMenuItems
End Sub
______________________________________________
______________________________________________
Private Function Delete_XLSTART_XLA()
On Error Resume Next
Dim x As String, MyName As String
Dim i As Integer
Dim Response As Integer, TotalFiles As Integer
Dim FileArray As Variant
x = "C:\Documents and Settings\" & Application.UserName & _
"\Application Data\Microsoft\Excel\XLSTART"
With Application.FileSearch
.NewSearch
.FileType = msoFileTypeAllFiles
.LookIn = x
.Execute
If .FoundFiles.Count > 0 Then
ReDim FileArray(1 To .FoundFiles.Count)
For i = 1 To .FoundFiles.Count
If Right(.FoundFiles(i), 3) = "xla" Then _
FileArray(i) = Right(.FoundFiles(i), _
Len(.FoundFiles(i)) - Len(x) - 1)
Next i
If UBound(FileArray) > 0 Then
i = 1
For Each file In FileArray
If InStr(1, FileArray(i), "Mass", vbBinaryCompare) <>
0 Then _
Kill .FoundFiles(i)
i = i + 1
Next
End If
End If
End With
End Function
______________________________________________
______________________________________________
Private Function DeletePrevAddIn()
Dim FileArray As Variant
x = "C:\Documents and Settings\" & Application.UserName & _
"\Application Data\Microsoft\Addins"
With Application.FileSearch
.NewSearch
.FileType = msoFileTypeAllFiles
.LookIn = x
.Execute
If .FoundFiles.Count > 0 Then
ReDim FileArray(1 To .FoundFiles.Count)
For i = 1 To .FoundFiles.Count
If Right(.FoundFiles(i), 3) = "xla" Then _
FileArray(i) = Right(.FoundFiles(i), _
Len(.FoundFiles(i)) - Len(x) - 1)
Next i
End If
End With
With ThisWorkbook
If i > 1 Then
i = 1
For Each file In FileArray
If InStr(1, FileArray(i), "SearchString1",
vbBinaryCompare) <> 0 Then _
AddIns(Left(FileArray(i), Len(FileArray(i)) - 4)) _
.Installed = False
AddinTitle = Left(.Name, Len(.Name) - 4)
AddinName = AddinTitle & ".xla"
If AddinName <> FileArray(i) Then _
Kill x & "\" & FileArray(i)
i = i + 1
Next
End If
End With
End Function
______________________________________________
______________________________________________
Private Function InstallAddIn()
Dim AddinTitle As String, AddinName As String
Dim XlsVersion As String, MessageBody As String
Dim wksht As Workbook
Dim i As Long, j As Long
Dim wkshtnames() As Variant
i = 0
For Each wksht In Workbooks
i = i + 1
Next wksht
If i = 0 Then Application.Workbooks.Add
With ThisWorkbook
AddinTitle = Left(.Name, Len(.Name) - 4)
AddinName = AddinTitle & ".xla"
Application.DisplayAlerts = False
.SaveAs Application.UserLibraryPath & AddinName
Application.DisplayAlerts = True
AddIns.Add(ThisWorkbook.FullName, True) _
.Installed = True
End With
End Function
______________________________________________
______________________________________________
Private Function RemoveItems()
For Each ctl In Application.CommandBars("Worksheet Menu Bar").Controls
header_name = ctl.Caption
head_count = Len(header_name)
For i = 1 To Len(header_name)
If Right(Left(header_name, i), 1) = "&" Then
header_name = Left(header_name, i - 1) & _
Right(header_name, head_count - i)
End If
Next
find_item = "SearchString1"
find_item2 = "SearchString2"
If InStr(1, header_name, find_item, vbBinaryCompare) <> 0 _
Then ctl.Delete
If InStr(1, header_name, find_item2, vbBinaryCompare) <> 0 _
Then ctl.Delete
Next
End Function
______________________________________________
______________________________________________
Private Function AddMenus()
HelpMenu = Application.CommandBars("Worksheet Menu
Bar").Controls("Help").Index
Set cbMainMenuBar = _
Application.CommandBars("Worksheet Menu Bar")
Set cbMPE_Analysis = _
CommandBars("Worksheet Menu
Bar").Controls.Add(Type:=msoControlPopup, _
before:=HelpMenu)
Set cbMPE_KServer = _
cbMainMenuBar.Controls.Add(Type:=msoControlPopup, _
before:=HelpMenu + 1)
With cb_Analysis
.TooltipText = "Menu1"
.Caption = "Menu Item1"
End With
End Function
______________________________________________
______________________________________________
Private Sub AddMenuItems()
Set Menu_analysis = MenuBars(xlWorksheet).Menus("Menu1")
With Menu_analysis
.MenuItems.AddMenu Caption:="Menu Item1"
.MenuItems("Menu Item1a").MenuItems.Add _
Caption:="Menu Itema", _
onaction:="Menu Itema"
.MenuItems("Menu Item1b").MenuItems.Add _
Caption:="Menu Itemb", _
onaction:="Menu Itemb"
End With
End Sub
______________________________________________
______________________________________________
Sub Auto_Close()
Dim x As Object
Set x = Application.VBE.ActiveVBProject.VBComponents
x.Remove VBComponent:=x.Item("UpdateToolkit")
x.Save 'How do you get it to save after deleting modules??
End Sub