I want to check if addin is installed and is referenced. The below code checks for add in is installed or not. How can i check if its referenced in excel.
By Refernced i mean is Tools > Addins > Addins Dailog box > If addins is installed > check if a addin with particular name is checked.
I would like preferably without any loop.
Sub Demo()
Dim b As Boolean
b = CheckAddin("Solver add-in")
MsgBox "Solver is " & IIf(b, "", "not ") & "installed"
End Sub
Function CheckAddin(s As String) As Boolean
Dim x As Variant
On Error Resume Next
x = AddIns(s).Installed
On Error Goto 0
If IsEmpty(x) Then
CheckAddin = False
Else
CheckAddin = True
End If
End Function
Sub Sample()
Dim wbAddin As Workbook
On Error Resume Next
Set wbAddin = Workbooks(AddIns("My Addin").Name)
If Err.Number <> 0 Then
On Error GoTo 0
'Set wbAddin = Workbooks.Open(AddIns("My Addin").FullName)
Debug.Print "Not Referenced"
Else
Debug.Print "Referenced"
End If
End Sub
You need to test is the addin is open, pretty much like any other workbood. This will return True if an addin is loaded:
Function AddinIsLoaded(AddinName As String) As Boolean
On Error Resume Next
AddinIsLoaded = Len(Workbooks(AddIns(AddinName).Name).Name) > 0
End Function
For example:
Sub Test
Debug.Print AddinIsLoaded("Solver add-in")
End Sub
I've had a problem that even when the function returns True, I would still get an error when trying to use that addin. It turns out, an addin can be installed, but not "open". So, in addition to checking for the addin, I also check if the addin file is open. If not, I open the addin. See my question and answer here:
Excel VBA Checking if Addin Is Installed But Not Open
Related
I use this code to install an Add-in. But it does not seem to enable it. I get this error message:
Runtime error 1004: Unable to set the installed property of the add-in
class.
My code:
Sub installatie_Click()
Dim AI As Excel.AddIn
Set AI = Application.AddIns.Add(Filename:="J:\Planning\Sjablonen\Updates\versieA.xlam")
Application.AddIns("versieA").Installed = True
End Sub
I always use to make my adding able to self install. Please try this code (in addin Workbook_Open event of its ThisWorkbook module):
Your file may have a problem...
You have to set its Title (BuiltinDocumentProperties(1)). Manually, right click on the addin file and modify (only with adding closed) or programatically (ThisWorkbook.BuiltinDocumentProperties(1) = "Whatever"), but without spaces..
Private Sub Workbook_Open()
Dim Name As String, tmp As Boolean, n As Boolean, Merk As String
Name = ThisWorkbook.BuiltinDocumentProperties(1)
On Error Resume Next
tmp = AddIns(Name).Installed
If Err.number <> 0 Then
Err.Clear: On Error GoTo 0
If Workbooks.Count = 0 Then n = True
If n Then
Workbooks.Add
Merk = ActiveWorkbook.Name
End If
AddIns.Add Filename:=ThisWorkbook.FullName
AddIns(Name).Installed = True
If n Then Workbooks(Merk).Close False
End If
On Error GoTo 0
End Sub
I have implemented a DragDrop functionality to my Excel database using TreeView control, using this code:
Private Sub TreeView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim StrPath As String
StrPath = Data.Files(1)
'path saved in UserForm label named "FilePathLB"
FilePathLB = StrPath
End Sub
It works perfectly fine on most of the machines I distributed the file to, however some machines with older versions of MS Office fire an error on the very first line (Private Sub ...) due to not being able to find Microsoft Windows Common Control library.
My question: is it possible to late bind this library and thereby preventing the error from happening?
Or at least, is it possible to add a debugger to prevent the error from showing, something like On Error Resume Next for the whole Sub? I understand that in this case the DragDrop function would not work, but it is better than an error.
For your last question:
Sub ()...
On Error GoTo ErrorHandler
'Your code
Exit Sub
ErrorHandler:
Msgbox "Could not load DragDrop function. Program execution has been terminated.", vbExclamation, "Error"
End Sub
If you want to, you could also just drop the MsgBox.
EDIT:
will not work as the code breaks on the first line.
Code below to support my comment. If an error occurs in the sub-macro, then the PassedSub variable won't be set to True, thus indicating an error.
Public PassedSub As Boolean
Sub test1()
On Error Resume Next
Call test2
If PassedSub = False Then GoTo ErrorHandler
On Error GoTo 0
Exit Sub
ErrorHandler:
MsgBox "Could not load DragDrop function. Program execution has been terminated.", vbExclamation, "Error"
End Sub
Sub test2()
Debug.Print 2 / 0
PassedSub = True
End Sub
I want to apply the error handling mechanism in Excel VBA, I want to catch this "runtime error 9", but it's not working.
I am using this userform_initialize() method/sub over and over again, each time I don't want to open this "SAMPLE UPDATE FILE.xlsm" workbook instead, I want to check if it's already open. if yes, then switch to that window or open that workbook.
I have tried on error resume next statement as well but still, it breaks on switching to window "Windows("SAMPLE UPDATE FILE.xlsm "). Select"
Private Sub UserForm_Initialize()
Application.DisplayAlerts = False
On Error GoTo OPEN_WB_ERR
Windows("SAMPLE UPDATE FILE.xlsm").Select
UserForm1.ComboBox1.RowSource = ("'X:\SAMPLE UPDATE FILE.xlsm'!SEARCH")
Windows("PROFORMA_INVOICE.xlsm").Activate
On Error GoTo 0
Exit Sub
OPEN_WB_ERR:
Workbooks.Open Filename:="X:\SAMPLE UPDATE FILE.xlsm"
UserForm1.ComboBox1.RowSource = ("'X:\SAMPLE UPDATE FILE.xlsm'!SEARCH")
Windows("PROFORMA_INVOICE.xlsm").Activate
Resume Next
End Sub
any advice will be helpful...
Check your setting in the VB editor (Tools >> Options >> General tab >> Error Trapping) for how errors are handled - if you have "Break on all errors" selected then it will always break regardless of any error handling you have set. "Break in Class module" is a good option.
Try,
Private Sub UserForm_Initialize()
Dim path As String, Fn As String
Dim Wb As Workbook
Fn = "X:\SAMPLE UPDATE FILE.xlsm"
Set Wb = Workbooks.Open(Filename:=Fn)
UserForm1.ComboBox1.RowSource = "'" & Fn & "'" & "!SEARCH"
ThisWorkbook.Activate
End Sub
The Initialize event procedure runs when the form is first created, before it is shown. You should open your workbook before creating the form, not as part of that process. Try a procedure like the one below, to be installed in a standard code module.
Sub OpenUserForm()
Dim MyForm As UserForm1
' open your workbook here
Set MyForm = New UserForm1 ' this fires the Initialize event
UserForm1.Show
' the code below runs when MyForm is closed
Unload MyForm
Set MyForm = Nothing
End Sub
Note that a form by the name of UserForm1 must exist. I recommend to give it another, more descriptive name. If you do that whatever name you give is the one to use in the Dim statement declaring MyForm.
I use a WorkbookIsOpen function
Public function WorkbookIsOpen(byval strFile as string) as Boolean
Dim wbkCurr as excel.workbook
WorkbookIsOpen = false
For each wbkCurr in application.Workbooks
If wbkCurr.name = strfile then
WorkbookIsOpen = true
Exit for
Endif
Next wbkCurr
End function
Pass just the file name and extension ie myworkbook.xlsx
Then I just adjust my logic accordingly
How can I successfully pass an error code from the VBA module in Part 2 to the VBScript that executed it in Part 1?
Part 1: VBScript that executes the VBA in Part 2
Option Explicit
Dim xlApp
Dim xlBook
If Err.Number <> 0 Then Err.Clear
Set xlApp = GetObject("","Excel.Application")
Set xlBook = xlApp.Workbooks.Open("\\(directory)\(File Name).xlsm", 0, True)
xlApp.Run "(Macro Name)"
If Err.Number <> 0 Then
Msgbox Err.Number & ": " & Err.Description & " The script will now quit."
WScript.Quit
Else
Msgbox Err.Number
End If
xlApp.Quit
Part 2: VBScript that executes the VBA in Part 2
Option Explicit
Sub (MacroName)()
'Snip - A bunch of working code
Err.Number = 7
Err.Description = "Made up error for testing - delete me"
End Sub
When this script is run, Err.Number is 0 when tested, and the Msgbox simply displays 0. What do I need to do to get it to display 7: Made up error for testing - delete me The script will now quit.
The IntelliSense doesn't show it, but Application.Run is a Function with a Variant return type, so you can have a macro like this:
Public Function Test() As Integer
Test = 42
End Function
And then the debug/immediate pane would output this:
?Application.Run("test")
42
Make (macro name) a function that returns the error number, and then instead of this:
xlApp.Run "(Macro Name)"
You could do that:
Dim result
result = xlApp.Run("(Macro Name)")
And then check for the value of result instead of that of Err.Number.
Now, if (macro name) is also supposed to be called as a macro by Excel, changing it to a Function will make your VBScript work, and will break the Excel macro.
You could make a "wrapper" function to leave the macro exposed as a Sub for Excel to use, and call the wrapper function from VBScript:
Function VBScriptMacroNameWrapper() As Integer
On Error Resume Next
' run macro sub here
VBScriptMacroNameWrapper = Err.Number
On Error GoTo 0
End Function
My Excel AddIn is written in C#, it uses Excel DNA, AddIn Express RTD, NetOffice
Installer is created with Advanced Installer, plus VBA
the VBA code is in install.xls
Private Sub Workbook_Open()
Dim quit As Integer
Dim added As Boolean
Add_Addin
If Workbooks.Count = 1 Then
Application.quit
Else
Me.Close
End If
End Sub
Private Sub Add_Addin()
On Error GoTo ERR_
Dim addinFile As String
addinFile = ThisWorkbook.Path & "\" & "MyAddIn.xll"
If Len(addinFile) > 0 Then
Dim LEA As AddIn
Set LEA = Application.AddIns.Add(addinFile)
If (Not LEA Is Nothing) Then
LEA.Installed = True
Else
MsgBox "Failed to add XLL"
End If
Else
MsgBox "XLL file not found"
End If
Exit Sub
ERR_:
MsgBox ("Error " & Err.Number & " " & Err.Description)
End Sub
Everything works fine. and I did not change installer
Now when one user installs new version of my addin,
when install.xls is run in Excel, a window pops up saying "insert smart card"
I think and think and figure out the only thing changed (compared with previous version) is digital sigature of the install.xls b/c the previous signature file expired recently
I signed install.xls with the new certificate
Now the strange window pops up during install.
Anyone know how to solve this?
Thanks
Perhaps you can try following the instructions on the link below for adding a trusted location.
http://office.microsoft.com/en-us/word-help/add-remove-or-modify-a-trusted-location-for-your-files-HA010354311.aspx#BM1
This is a fix I am pursuing for a similar issue.