VBScript to add a Macro in Excel
Hey guys,
Is there a reason why this:
Will work when run manually via a VBS file but will not work if used via a Embedded VBScript Custom Action and/or Execute VBScript custom action either.
Now, I'm in NO way a VB expert, I fouind this piece of code on Google, tried it and it worked, hence why I was trying to include it in my package. If there are any problems with it, let me know how I could improve it please?
Thanks!
Stephane
Is there a reason why this:
Dim oXL, oAddin
Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Add
Set oAddin = oXL.AddIns.Add("C:\Program Files\Datastream\Datastream Advance\AdvanceOffice.xla", True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
Will work when run manually via a VBS file but will not work if used via a Embedded VBScript Custom Action and/or Execute VBScript custom action either.
Now, I'm in NO way a VB expert, I fouind this piece of code on Google, tried it and it worked, hence why I was trying to include it in my package. If there are any problems with it, let me know how I could improve it please?
Thanks!
Stephane
0 Comments
[ + ] Show comments
Answers (7)
Please log in to answer
Posted by:
anonymous_9363
15 years ago
Stephane, what error do you get when executing as a CA?
let me know how I could improve it please?Easy! Start again. That code is dreadful. No error-trapping, no 'Option Explicit'....hard-coded paths...ugh....Someone posted a reasonable script here on AppDeploy which included adding AND removing XL add-ins. Have a search for it.
Posted by:
Fau
15 years ago
Wow are you serious?
This is what I hate from VB code found on Google, and not being an expert, well I rarely spot dreadful code. However, I will have a look on these message boards for the post you speak of.
You did ask for the error message. I don't see any when I install the package, that's what's odd. However, digging through the log file, searching for my CA, I see this:
And further down...
Now, unless I'm really blind, I don't see anything that stands out. (In this occurence, my CA runs a local VB Script from installed files.)
I'll let you know if I find it.
Thanks Ian!
Stephane
This is what I hate from VB code found on Google, and not being an expert, well I rarely spot dreadful code. However, I will have a look on these message boards for the post you speak of.
You did ask for the error message. I don't see any when I install the package, that's what's odd. However, digging through the log file, searching for my CA, I see this:
MSI (s) (40:30) [13:01:29:537]: Doing action: Excel_Addin
Action ended 13:01:29: PublishProduct. Return value 1.
MSI (s) (40:30) [13:01:29:537]: Transforming table CustomAction.
MSI (s) (40:30) [13:01:29:537]: Transforming table CustomAction.
Action start 13:01:29: Excel_Addin.
MSI (s) (40:30) [13:01:29:537]: Doing action: InstallFinalize
Action ended 13:01:29: Excel_Addin. Return value 1.
Action start 13:01:29: InstallFinalize.
And further down...
MSI (s) (40:30) [13:02:35:412]: Executing op: ActionStart(Name=Excel_Addin,,)
MSI (s) (40:30) [13:02:35:412]: Executing op: CustomActionSchedule(Action=Excel_Addin,ActionType=3158,Source=C:\Program Files\Datastream\Datastream Advance\ExcelMacro.vbs,,)
Now, unless I'm really blind, I don't see anything that stands out. (In this occurence, my CA runs a local VB Script from installed files.)
I'll let you know if I find it.
Thanks Ian!
Stephane
Posted by:
anonymous_9363
15 years ago
Posted by:
Fau
15 years ago
Hello Ian,
Sadly, the answer is no. It doesn't appear in Excel under the Addins menu. And naturally, doesn't seem to be installed.
I also found this script posted by "Scripting Machine" if I remember correctly.
However, like you mentionned in your initial response, there's still no "on error resume" or nothing of the kind. So I'm not sure if this script is better either.
But... This script still doesn't add my macro in Excel either via Executed VBS file or Embedded VBS code.
I'll keep on looking!
Stephane
Sadly, the answer is no. It doesn't appear in Excel under the Addins menu. And naturally, doesn't seem to be installed.
I also found this script posted by "Scripting Machine" if I remember correctly.
Dim strXLApath : strXLApath = "C:\Program Files\Datastream\Datastream Advance\AdvanceOffice.xla"
'Call RemoveXLA(strXLApath)
Call AddXLA(strXLApath)
Sub AddXLA(strXLApath)
Dim oXL : Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Add
Dim oAddin : Set oAddin = oXL.AddIns.Add(strXLApath, True)
oAddin.Installed = True
oXL.Quit
Set oAddin = Nothing
Set oXL = Nothing
End Sub
Sub RemoveXLA(strXLApath)
Dim oXL : Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Add
Dim oAddin : Set oAddin = oXL.AddIns.Add(strXLApath, False)
oAddin.Installed = False
oXL.Quit
Set oAddin = Nothing
Set oXL = Nothing
End Sub
However, like you mentionned in your initial response, there's still no "on error resume" or nothing of the kind. So I'm not sure if this script is better either.
But... This script still doesn't add my macro in Excel either via Executed VBS file or Embedded VBS code.
I'll keep on looking!
Stephane
Posted by:
Fau
15 years ago
Posted by:
anonymous_9363
15 years ago
I'm not sure if this script is better either.The fact that almost 100% of the code is identical in both functions should provide a clue as to its quality LOL.
Here's a quick test you can do - set up XL with low macro security and record a macro where you add the add-in. Take that code and paste it into your editor. Remove all the data-typing (i.e. where variables are declared "As String" or "As Object", etc) and compare it to what you have. I can't *see* anything missing but sometimes, the commas and quotes get hard to see when you're looking so hard!
[EDIT 16:10]On the above note, something nagging at the back of my mind suggests that you can either use the .Installed method on the same line as the .Add method or, if using separate lines, you need to specify the XLA's title not its name, as in:
oAddin("Whatever text appears in Excel's add-ins list goes here").Installed = True
[/EDIT]
Posted by:
pgiesbergen
15 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.