Deployment of Excel addins - excel

What is a nice way to install Excel addins?
I want to easily push out Excel addins and it's dependent files that can all be stored in one directory. I want the users to get a never version on request or I want to push it out to their PCs.
This is inside of a company where I have full control over the environment.

you can insert this code in your *.xlam in the "ThisWorkBook" module
this code install and activate the current AddIns
Private Sub Workbook_Open()
Dim oXL As Object, oAddin As Object
URL = Me.Path & "\"
normalUrl = Application.UserLibraryPath ' Environ("AppData") & "\Microsoft\AddIns"
AddinTitle = Mid(Me.Name, 1, Len(Me.Name) - 5)
If URL <> normalUrl Then
If MsgBox("Can you Install AddIns ?", vbYesNo) = vbYes Then
Set oXL = Application ' CreateObject("Excel.Application")
oXL.Workbooks.Add
Me.SaveCopyAs normalUrl & Me.Name
Set oAddin = oXL.AddIns.Add(normalUrl & Me.Name, True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
End If
End If
End Sub

Assuming you are trying to deploy an addin solution which you have created. You would take the set of excel macros you want in the addin and save it to an xlsm excel file in 2007/2010 or simply an xls file for 2003. From there go to your VBA Project Window (Alt + F11) and click once on the "ThisWorkBook" object as shown below and change the workbook properties to become an excel addin by changing the IsAddin to True.
Keep in mind this will remove the excel sheets from view as it becomes an xla or xlam extension. DOn't panic, you just made your excel workbook into an addin. Save it in the top left corner save icon and you have created your addin. You can follow the below procedures to link your addin to other workbooks.
After that, you must be aware of where you saved the excel file because that will the location of the addin file (since they are one and the same).
Now, open an excel file you want this addin connected to.
From there go to the vba project window again and go to tools and reference and select the excel addin you just made.

The approach I use in my company is to store the addin files (and any other dependant files) in an svn repository. Users check out the relavent folder to their machine to a consistent location (can be a bit like herding cats to get everyone to use the same folder structure).
When an update is available simply advise users to update their svn checkout folder.

Related

How to deal with co-authoring while editing an Excel file in Sharepoint via VBA

I have an excel file stored in Sharepoint (which is also accessible with Microsoft Teams), with the path: https://organization.sharepoint.com/PathOfFile/myFile.xlsx
The file can be edited by multiple at the same time with the co-authoring feature in Sharepoint.
I want to use another excel file stored locally in my computer to access and modify the one in Sharepoint. This local file has a button with this VBA code in it:
Sub UpdateSP():
f_name = "https://organization.sharepoint.com/PathOfFile/myFile.xlsx"
Workbooks.Open f_name
Workbooks("myFile.xlsx").Activate
ActiveWorkbook.Sheets("sheet1").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.Value = 9999
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = 0000
ActiveWorkbook.Close SaveChanges:=True
End Sub
In principle it works, the file in Sharepoint is modified. But things go wrong if there's someone editing the file while I run the code, then two versions of the file seem to be created, one for the online-live editing, and the one for my code.
If this happens, the online version of the file won't show the changes made by the code, and whenever the file is opened with the excel app, a pop-up will show asking which version of the file should be kept, losing all the changes done in the disposed version.
I have tried to use the CanCheckOut and CheckOut methods, but CanCheckOut always returns False for whatever reason (there are some questions here with the same issue but I havent been able to find a solution).
Can someone suggest a solution to this issue? Thanks.
I'm not 100% sure it will work on SharePoint, but in theory, ADODB is a library for VBA that has the syntax of objects to use Microsoft's Jet Engine so you can open files AdLockOptimistic---ally. ((look up lock types in ADO.net))
This works on a file directory basis, so if the DB being modified is open, it will handle the update.
Instead of using Excel's Application to open the file, you would establish an ADO connection, and then specify the type of Lock in order to access the Excel's sheets and tables inside it.
This works for shared / network drives, so I'm guessing since SharePoint can be mapped as a file explorer drive, then ADO should work and is worth a try.
Here's a basic example to get you started: ADO question
Try enabling the autosave after activating the workbook.
To do so, add this line:
ActiveWorkbook.AutoSaveOn = True
after the Workbooks("myFile.xlsx").Activate line.
I have had similar issues with collaborative files and making sure the autosave is enabled has solved it.
To be able to incorporate changes that way your code must run inside a coauthoring context.
Instead of opening the document from another doc or local copy, the code must be running inside the same document being opened from the same source URL (Sharepoint or OneDrive), that way the add-in or macro can make changes that Excel itself will handle on a coauthoring context.
I recommend taking a look at Coauthoring in Excel add-ins of the Office Dev Center, including the linked articles inside (specifically "coauthoring", redirecting to the support center, and "About coauthoring in Excel (VBA)" at the bottom with more samples).
CanCheckOut will always return false if a workbook is open. Thus you must check before you touch it. The CheckOut command will not open the file so we must also have an open statement after CheckOut.
Using your example it would look like this;
Option Explicit
Public Sub UpdateSP()
Dim fName As String
fName = "https://organization.sharepoint.com/PathOfFile/myFile.xlsx"
If Workbooks.CanCheckOut(fName) Then
Workbooks.CheckOut fName
Dim myFile As Workbook
Set myFile = Workbooks.Open(fName)
Dim mySheet As Worksheet
Set mySheet = myFile.Sheets("Sheet1")
Dim startRange As Range
Set startRange = mySheet.Range("A" & mySheet.Rows.Count).End(xlUp).Offset(1)
startRange.Value = 9999
startRange.Offset(0, 1).Value = 0
myFile.Close SaveChanges:=True
Else
MsgBox fName & " can't be checked out at this time.", vbInformation
End If
End Sub

Open Excel file in SharePoint through VBA

I'm trying to open an excel file in SharePoint through a vba code in another local excel file. However, it gives me a Dialog Box and lets me save the file instead of directly opening in Excel.
Below is the code I used. Would be very grateful for your help. Thanks.
Dim wb AS Workbook
FilePath = "https://company.sharepoint.com/sites/Folder1/Folder2/Filename.xlsm"
Set wb = Workbooks.Open(Filename:=FilePath, UpdateLinks:=0)
I had this issue also. First, you have to fix something in the excel options. So, go to File > Options > Advanced > then scroll down until you see Link Handling > then select the box that says "Open Supported hyperlinks to Office in Office Desktop Apps". Next, I used the coding below to get the link to work. you just need to reply the part of the coding with you sites url sharepoint file. the only issue I came up with is that you have to run the coding below, wait for the file to completely load, and then you can run macros on this file.
Sub OPEN_YRLY()
'
' OPEN_YRLY Macro
'
'
Range("C1").Select
ActiveWorkbook.FollowHyperlink Address:="https://aramark365-my.sharepoint.com/:x:/r/personal/......", NewWindow:=False, AddHistory:=True
End Sub

SSIS Script Task Not Running Excel Macro With AddIn

I have an SSIS Script Task that calls an Excel workbook macro. This Excel macro uses an Excel AddIn (xlam) for logging. When the code runs it blows up unless during debugging, I put a breakpoint in the workbook, and then step through the Excel part. If I step through it once, my process then can run via SQL Server Agent, and in automation again. Any idea why I need to put in a breakpoint to fix the process?
Here is my sample SSIS Script Task Code:
Dim xlApp As Excel.Application = Nothing
'
Try
xlApp = New Excel.Application
xlApp.Visible = True
xlApp.Application.DisplayAlerts = False
xlApp.Workbooks.Open(strExcelDriverFile)
xlApp.Run("CreateReport")
Dts.TaskResult = ScriptResults.Success
xlApp.Workbooks.Close()
xlApp.Quit()
Catch ex As Exception
xlApp.Quit()
Dts.TaskResult = ScriptResults.Failure
End Try
Here is my sample Excel Code:
Sub CreateReport
'Log using the addin
LogHWMsg Replace(Replace("Starting Macro", "'", ""), """", ""), GExecGuid, 0, 1, ThisWorkbook.Path, ThisWorkbook.Name, _
ActiveWorkbook.Path + "\" + ActiveWorkbook.Name, Environ("UserName")
'Do some stuff...
End Sub
Excel Automation does automatically not load add-ins. You need to specifically load them via code.
The following example is from Loading Excel Add-Ins at Runtime (Written by: Jeremy Espenshade)
The following options are available if you want to do this.
1.) Application.RegisterXLL
a. This is a method which can be called from VBA which loads an XLL at a specific location and registers the functions and commands contained in the XLL.
2.) AddIns.Add
a. This is a method which can be called from VBA which loads any type of add-in (XLL, XLA or XLAM). After loading the add-in, perform step 3 to open it.
3.) AddIn.Installed = true
a. Once you have a reference to a loaded add-in, set AddIn.Installed = true to cause the add-in to be opened.
b. Note that add-ins that are known when Excel is started with the /automation switch will already be marked as "Installed", but they are not opened. In this case, Set Installed = false before setting Installed = true
Private Sub Workbook_Open()
Dim success As Boolean
Dim myAddIn As AddIn
' Load XLL
success = Application.RegisterXLL("c:\myaddins\myxll.xll")
' Load and install new XLAM
Set myAddIn = Application.AddIns.Add("c:\myaddins\myxlam.xlam")
myAddIn.Installed = True
' Load known XLAM
For Each myAddIn In AddIns
If myAddIn.Name = "myknownaddin.xlam" Then
myAddIn.Installed = False
myAddIn.Installed = True
End If
Next
End Sub
Edit:
The OP has asked that I include the technique that worked for him; i.e. activating the add-ins that are available to the user account under which Excel is executing.
The AddIns object is a property of the Excel.Application object. When you are using Excel automation, the Excel application will not automatically load the AddIns that would be loaded in an interactive session. Therefore, you need to use the proper technique demonstrated above to load the add-in based on its type.
Example Activating Known Addins:
xlApp = New Excel.Application
For Each addin As Excel.AddIn In xlApp.AddIns
If Not String.IsNullOrWhiteSpace(addin.Path) Then
addin.Installed = True
End If
Next
Please take not that the code verifies that the Path property is not empty. This can be cause by an add-in that was uninstalled.
Excel is also a pain about maintaining links to previously loaded add-ins. In fact the only way to remove on from the list is to make the the file unavailable from the originally loaded path and to specifically ask Excel to remove it via a dialog window once Excel can not find the add-in. Therefore, a Known add-in may not be accessible. To make this worse, setting the Installed property on an inaccessible add-in does not throw an error.
However an error will be thrown if you try to access any member of the unloadable add-in.

How can I activate an Excel add-in from the command line in Windows 7?

Currently I'm writting VB functions and save them as an Excel addin .xlam file.
I want to have a .bat script so as to quickly deploy those addins.
Currently, to activate my .xlam addins, I have to Open Excel - File - Option - Addins - Browse to addin files... as below screenshot. This is absolutely manual, repeated & tiring thing to do.
So my need is to automate the activation process.
I was looking for exactly the same sort of thing this morning. I will eventually try something like this out, but I haven't yet. So, here is what I have come to so far:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins2.add.aspx
This is an example about how to use Excel automation from C#. From what I see, all these automation interfaces are really COM interfaces, so you are not restricted to C# or Visual Basic (maybe you can use some fancy scripting of Windows to work with them? what I will try is to use python with pywin32, but that's only because it suits my taste).
Then, for registering the addin(s), check this method:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins2.add.aspx
I actually saw an example somewhere about how to use it, but I can't find it right now.
Anyway, these are just ideas. I'm very interested on knowing how it all ends ;-)
you can insert this code in your *.xlam in the sheet "ThisWorkBook" this code install and activate the current AddIns, just by opening
Private Sub Workbook_Open()
Dim oXL As Object, oAddin As Object
URL = Me.Path & "\"
normalUrl = Application.UserLibraryPath ' Environ("AppData") & "\Microsoft\AddIns"
AddinTitle = Mid(Me.Name, 1, Len(Me.Name) - 5)
If URL <> normalUrl Then
If MsgBox("Can you Install AddIns ?", vbYesNo) = vbYes Then
Set oXL = Application ' CreateObject("Excel.Application")
oXL.Workbooks.Add
Me.SaveCopyAs normalUrl & Me.Name
Set oAddin = oXL.AddIns.Add(normalUrl & Me.Name, True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
End If
End If
End Sub
After one manually added time, we can update the addin by copy the addin file to Excel addin lair. Here is the .bat script to do it.
set fipAddin=".\FIPphase2.xlam"
set excelAddinLair="%APPDATA%\Microsoft\AddIns"
copy %fipAddin% %excelAddinLair%
Hope it helps!

How to get User Defined Functions in Excel Add-Ins (Excel 2007) to work with AutoComplete?

I have created an Add-In and when I go to use it in the expressions box the autocomplete doesn't work. I do see them when I click the functions 'f' button under user defined functions. I would just like the AutoComplete to work with them so I don't have to memorize their names or have to click the functions 'f' button each time.
Although this is an old thread, there seems to be very little solutions out there. I have found an example over at JKP Application Development Services originally found by Laurent Longre. One caveat is explained below:
Disadvantage of this trick method, is that one is actually re-registering a function within the dll one uses, which might be used by any program
http://www.jkp-ads.com/Articles/RegisterUDF01.asp
This solution only registers/un-registers the UDF, but the user will still have to save the workbook as an .xlam and install the addin. I used the following code to automatically install the current workbook as an Excel addin (if you are going to be updating the addin, you'll need to add some error catching to determine if the addin is already installed).
'Saves current workbook as an .xlam file
sFile = Application.LibraryPath & "\" & "name_of_addin" & ".xlam"
ThisWorkbook.SaveAs sFile, 55
ThisWorkbook.IsAddin = True
'Adds temporary workbook
Workbooks.Add
'Installs the addin
Set oAddin = AddIns.Add(sFile , False)
oAddin.Installed = True
'Closes temporary workbook
Workbooks(Workbooks.Count).Close
MsgBox ("Installation Successful. Please close Excel and restart.")
'Closes workbook without saving
Workbooks(sFirstFile).Close False
AFAIK there is no way (unfortunately) in current Excel versions including Excel 2010 to make a UDF use Autocomplete. There are ways to add argument descriptions and help for the function wizard.

Resources