Re: Auto Load Add-In



You're loosing me! Is this thread about loading an addin, or adding a
references.

Anyway -

ThisWorkbook.VBProject.References.AddFromFile _
"C:\data\test_addin.xla"

The above looks fine, assuming the ref does not already exist and a valid
project in the path.

Dim tempStr As String
tempStr = "C:\data\test_addin.xla"
If Dir(tempStr) = "" Then
MsgBox "you do not have test_addin.xla installed"
End
End If

The message implies the addin is not in the addins collection rather than
not found in the specified folder.

Avoid the "End" statement. If necessary release any object variables and
use Exit Sub or Function, possibly all the way back up to the original
calling routine.


If AddIns("test_AddIn").Installed =

Before the above, unless certain the addin exists in the addins collection
do -

On error resume next
Set adn = addins(the-Addin's-Title)
If adn is nothing then
Set adn = addins.add(C:\data\test_addin.xla")
End if

If not adn.installed then adn.installed = True


Normally once the reference has been added to a wb, when the wb opens the
referenced wb will open automatically.

Regards,
Peter T


"scott" <sbailey@xxxxxxxxxxxxxxx> wrote in message
news:O3iuPgQ5HHA.1208@xxxxxxxxxxxxxxxxxxxxxxx
I re-wrote my code into several subs listed below. I'd really like to
learn
the method of adding the addin to the references. Do you have any code to
accomplish this?

I tried the sub loadAddIn() below IN CODE #1 and I get ERROR #1. If I call
the sub, TestAddIn() in CODE #2, I get ERROR #2. Excel won't even allow my
code in CODE #2 to test if the add-in is loaded.

Basically, I'm striking out no matter what I try. I know there must be a
way
to accomplish this feat.

Note: I did manage to properly name my project "test_AddIn" instead of the
default "VBAProject".


CODE 1: *************************

Sub loadAddIn()

ThisWorkbook.VBProject.References.AddFromFile _
"C:\data\test_addin.xla"
End Sub

ERROR 1: *************************

Run-time error '1004':
Method 'VBProject' of object '_Workbook' failed


CODE 2: *************************


Sub TestAddIn()

Dim tempStr As String
tempStr = "C:\data\test_addin.xla"
If Dir(tempStr) = "" Then
MsgBox "you do not have test_addin.xla installed"
End
End If

If AddIns("test_AddIn").Installed = False _
Then AddIns("test_AddIn").Installed = True

Dim bFound As Boolean
Dim obj
'see if there is a reference already
For Each obj In ThisWorkbook.VBProject.References
MsgBox "name: " & UCase(obj.Name)
If UCase(obj.Name) = "test_addin.xla" Then
bFound = True
Exit For
End If
Next obj

'if no reference then set a reference.
If bFound = False Then _
ThisWorkbook.VBProject.References.AddFromFile _
"C:\data\test_addin.xla"
End Sub


ERROR 2: *************************

Run-time error '9':
Subscript out of range




"Peter T" <peter_t@discussions> wrote in message
news:%23KUmcMQ5HHA.4164@xxxxxxxxxxxxxxxxxxxxxxx
Looks like you don't particularly need to add the addin to the addin's
collection so just open and close it like a normal workbook, eg

Sub OpenCloseAddin(bOpen as boolean)
dim sFile as string
dim wb as workbook

sFile = "C:\data\test_addin.xla"
On error resume next
set wb = application.workboooks(sFile)
On error goto errH
if bOpen then
if wb is nothing then
application.workbooks.open sFile
end if
elseif not wb is nothing then
wb.close
end if
exit sub
errH:
if bOpen then
msgbox "Problem opening " & sFile
end if
end sub

warning - aircode !

Regards,
Peter T

"scott" <sbailey@xxxxxxxxxxxxxxx> wrote in message
news:#CwdemP5HHA.4712@xxxxxxxxxxxxxxxxxxxxxxx
I'm trying to load an add-in when my workbook opens. First I created an
add-in at "C:\data\test_addin.xla" that contains the code found below
in
"CODE 2" section. Next, I added code found below in "CODE 1" section to
the
file "C:\data\test.xls". While the add-in was open, in the Module
property,
I named the module "test_AddIn".

If I open "C:\data\test.xls" containing "CODE 1", I get a "Subscript
out
of
range" error and trace the error to the line

If AddIns("test_AddIn").Installed = False

in "CODE 1". To further troubleshoot, I manually added the add-in
within
Excel's References and noticed the name of my add-in within References
is
called "VBAProject".

I asume, the error is being caused because of the "VBAProject" name of
my
add-in.

QUESTIONS:

1. How can I change the "VBAProject" name to "test_AddIn".
2. Will changing the name as described in QUESTION #1 allow my add-in
to
load when the "C:\data\test.xls" workbook opens?
3. If I'm going about this wrong, can someone shed some light on how to
accomplish this task?



' CODE 1: ****************************

Sub Auto_Open()

Dim tempStr As String
tempStr = "C:\data\test_addin.xla"
If Dir(tempStr) = "" Then
MsgBox "You do not have the Test Add-In installed."
End
End If

If AddIns("test_AddIn").Installed = False _
Then AddIns("test_AddIn").Installed = True

End Sub

Sub Auto_Close()

If AddIns("test_AddIn").Installed = True _
Then AddIns("test_AddIn").Installed = False

End Sub


' CODE 2: ****************************

Sub Auto_Open()
MsgBox "Add-In Loaded"
End Sub









.


Loading