Results 1 to 15 of 15

Thread: [RESOLVED] runing an addin macro

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Resolved [RESOLVED] runing an addin macro

    I recorded the following excel macro:

    VB Code:
    1. Application.Run "ATPVBAEN.XLA!Histogram", ActiveSheet.Range("$G$4:$G$8"), _
    2.         "NewName", , False, True, True, False

    I want to do this programatically from VB.

    Here is my code:

    VB Code:
    1. Set wAddin = OutputSheet.Application.Workbooks.Open(AddIns(1).FullName)
    2.  
    3.      Application.AddIns(AddIns(1).Title).Application.Run "Histogram", Sheet1.Range(strTemp), _
    4.         "NewName", , False, True, True, False

    I get the error "The macro "Histogram" cannot found". I also tried a ! preceding Histogram ... same results ..

    How can I make this work?

    Thanks in advance for any help!
    Last edited by Muddy; Aug 17th, 2005 at 07:36 AM. Reason: RESOLVED

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: runing an addin macro

    here some exact code that causes the problem ... ANY help with this will be greatly appreciated ...
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim objXL As New Excel.Application
    3. Dim mySheet As Object
    4. objXL.Visible = True
    5. Set mySheet = objXL.Workbooks.Add.worksheets(1)
    6. objXL.Workbooks.Open (AddIns(1).FullName)
    7. objXL.Run AddIns(1).FullName & "Histogram", mySheet.Range("$G$4:$G$8"), _
    8.         "Hist", , False, True, True, False
    9. objXL.Quit
    10. Set objXL = Nothing
    11. Set mySheet = Nothing
    12. End Sub

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: runing an addin macro

    When specifying the macro to run you need to either specify just the macro name, or the filename and macro name with a ! between them.

    In your last post you appear to be missing the ! again, I think it should be:
    VB Code:
    1. objXL.Run AddIns(1).FullName & "!Histogram", mySheet.Range("$G$4:$G$8"), _
    2.         "Hist", , False, True, True, False

    This is of course assuming that AddIns(1).FullName is something which is valid within your VB program, rather than an Excel object you are trying to use.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: runing an addin macro

    Thanks for the reply ... but that doesnt work either ...

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: runing an addin macro

    Ok.. what exactly is AddIns(1)?

    Is this something specific to your program, or something in Excel?

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: runing an addin macro

    Addins is an excel object
    Addins(1) is ANALYS32.XLL ... I actually should have posted the code with Addins(2) which is ATPVBAEN.XLA ... but neither work for me. The order might be different on different machines ... I dont know ...

    you can run the following code from any Excel Workbook VBA Project to see all the addins available:
    VB Code:
    1. Sub ListAddins()
    2. Dim i As Integer
    3. For i = 1 To AddIns.Count
    4.     MsgBox AddIns(i).FullName
    5. Next i
    6. End Sub
    Thanks for the responses, btw!

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: runing an addin macro

    I see, I thought as much..

    Any Excel objects you use from VB must be qualified with a parent object (Application, Workbook, etc), or VB doesn't know what they are - sometimes it will use another object in a different object library (such as Word, if it is referenced) with the same name, which can obviously cause a few problems.

    I presume you haven't got Option Explicit enabled in your VB code, as this is the sort of issue that it should pick up on when you try to run the program.

    After some quick digging I can see that Addins is a child of the Application object, so you should be using code like this:
    VB Code:
    1. MsgBox objXL.AddIns(i).FullName

    As you have noticed, the Addins may not be in the same order on another machine, so you should use an expanded version of the code in your previous post to find out which one it is, eg:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim objXL As Excel.Application
    3. Dim mySheet As Object
    4.  
    5. Set objXL = New Excel.Application  '(using an extra line for the New makes all code which uses objXL a bit faster)
    6. objXL.Visible = True
    7.  
    8. Dim iCount As Integer               'Find the Addin
    9. Dim iAddin as Integer
    10. iAddin = -1
    11. For iCount = 1 To objXP.AddIns.Count
    12.    If UCase(objXP.AddIns(i).Name) = "ATPVBAEN.XLA"
    13.       iAddin = iCount
    14.       Exit For
    15.    End If
    16. Next iCount
    17. If iAddin = -1 Then   '(warn if couldn't find it)
    18.   MsgBox "Could not load the addin!"
    19. Else
    20.   Set mySheet = objXL.Workbooks.Add.worksheets(1)
    21.   objXL.Workbooks.Open (objXL.AddIns(iAddin).FullName)
    22.   objXL.Run objXL.AddIns(iAddin).FullName & "!Histogram",   mySheet.Range("$G$4:$G$8"), _
    23.         "Hist", , False, True, True, False
    24. End If
    25.  
    26. '(see note below)
    27.  
    28. Set mySheet = Nothing  'slight re-ordering here to help Excel close properly
    29. objXL.Quit
    30. Set objXL = Nothing
    31. End Sub
    Note - I know this is based on what was probably just example code, but you haven't got any code here to close the added Workbook (or the Addin), which could cause Excel to stay open in the background.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: runing an addin macro

    Thank Si ... but I still get the same error ... below is your code with some typos fixed ... but i still get that same error ...

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim objXL As Excel.Application
    3. Dim mySheet As Object
    4.  
    5. Set objXL = New Excel.Application  '(using an extra line for the New makes all code which uses objXL a bit faster)
    6. objXL.Visible = True
    7.  
    8. Dim iCount As Integer               'Find the Addin
    9. Dim iAddin As Integer
    10. iAddin = -1
    11. For iCount = 1 To objXL.AddIns.Count
    12.    If UCase(objXL.AddIns(iCount).Name) = "ATPVBAEN.XLA" Then
    13.       iAddin = iCount
    14.       Exit For
    15.    End If
    16. Next iCount
    17. If iAddin = -1 Then   '(warn if couldn't find it)
    18.   MsgBox "Could not load the addin!"
    19. Else
    20.   Set mySheet = objXL.Workbooks.Add.worksheets(1)
    21.   objXL.Workbooks.Open (objXL.AddIns(iAddin).FullName)
    22.   objXL.Run objXL.AddIns(iAddin).FullName & "!Histogram", mySheet.Range("$G$4:$G$8"), _
    23.         "Hist", , False, True, True, False
    24. End If
    25.  
    26. '(see note below)
    27.  
    28. Set mySheet = Nothing  'slight re-ordering here to help Excel close properly
    29. objXL.Quit
    30. Set objXL = Nothing
    31. End Sub

  9. #9
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: runing an addin macro

    Muddy,

    You are setting the object mySheet to a new spreadsheet and then attempting to open the addin??

    The addin will be already open but not installed if it is found within the addin list.. you will need to install it rather than open it, once loaded you do not need to refer to the fullname of the addin.. just Run "Histogram" followed by parameters will do the job..

    VB Code:
    1. Private Sub Command1_Click()
    2.   Dim objXL As Excel.Application
    3.   Dim objAdd as Excel.Addin
    4.   Dim myWb As Excel.Workbook
    5.   Dim mySheet As WorkSheet
    6.  
    7.   Set objXL = New Excel.Application  '(using an extra line for the New makes all code which uses objXL a bit faster)
    8.   objXL.Visible = True
    9.  
    10. 'get addin position
    11.   Dim i As Integer
    12.   For i = 1 To objXL.AddIns.Count
    13.     If objXL.AddIns(i).Name = "ATPVBAEN.XLA" Then
    14.         Set objAdd = objXL.AddIns(i)
    15.         Exit For
    16.     End If
    17.   Next i
    18.   If i > objXL.AddIns.Count Then GoTo NotAvailable
    19.   If not objAdd.Installed Then
    20.     objXL.Addins.Add(objAdd.FullName).Installed = True
    21.   End If
    22.   On Error Resume Next
    23.   Set myWb = objXL.Workbooks.Add
    24.   Set mySheet = myWB.Sheets("Sheet1")
    25.   'I'm presuming that there is some data input at this point, other wise this will fail due to no data..
    26.   objXL.Run "Histogram", mySheet.Range("$G$4:$G$8"), _
    27.         "Hist", , False, True, True, False
    28. End If
    29.  
    30. '(see note below)
    31.  
    32. Set mySheet = Nothing  'slight re-ordering here to help Excel close properly
    33. Set myWb = Nothing
    34. Set objAdd = Nothing
    35. objXL.Quit
    36. Set objXL = Nothing
    37. End Sub

    Give that a shot and post back if no joy..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: runing an addin macro

    Thanks for the advice, but I still get the same error ... I corrected some typos in the code you posted (also added some code to write some data into the sheet) and still get the error ...
    VB Code:
    1. Private Sub Command1_Click()
    2.   Dim objXL As Excel.Application
    3.   Dim objAdd As Excel.Addin
    4.   Dim myWb As Excel.Workbook
    5.   Dim mySheet As WorkSheet
    6.  
    7.   Set objXL = New Excel.Application  '(using an extra line for the New makes all code which uses objXL a bit faster)
    8.   objXL.Visible = True
    9.  
    10. 'get addin position
    11.   Dim i As Integer
    12.   For i = 1 To objXL.Addins.Count
    13.     If objXL.Addins(i).Name = "ATPVBAEN.XLA" Then
    14.         Set objAdd = objXL.Addins(i)
    15.         Exit For
    16.     End If
    17.   Next i
    18.   'If i > objXL.Addins.Count Then GoTo NotAvailable
    19.   If Not objAdd.Installed Then
    20.     objXL.Addins.Add(objAdd.FullName).Installed = True
    21.   End If
    22.   'On Error Resume Next
    23.   Set myWb = objXL.Workbooks.Add
    24.   Set mySheet = myWb.Sheets("Sheet1")
    25.   mySheet.Cells(4, 7).Value = 1
    26.   mySheet.Cells(5, 7).Value = 11
    27.   mySheet.Cells(6, 7).Value = 11
    28.   mySheet.Cells(7, 7).Value = 15
    29.   mySheet.Cells(8, 7).Value = 1
    30. objXL.DisplayAlerts = False
    31. myWb.Save
    32. objXL.DisplayAlerts = True
    33.   'I'm presuming that there is some data input at this point, other wise this will fail due to no data..
    34.   objXL.Run "Histogram", mySheet.Range("$G$4:$G$8"), _
    35.         "Hist", , False, True, True, False
    36.  
    37.  
    38. '(see note below)
    39.  
    40. Set mySheet = Nothing  'slight re-ordering here to help Excel close properly
    41. Set myWb = Nothing
    42. Set objAdd = Nothing
    43. objXL.Quit
    44. Set objXL = Nothing
    45. End Sub

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: runing an addin macro

    Try preceeding "Histogram" with it's parent object (maybe objXL.).

    I had posted something along these lines a few days back. Ie calling a presaved Excel Macro from within VB (once the Excel Object was loaded).

    Edit: Here is the Link (for what it's worth ):
    http://www.vbforums.com/showthread.php?t=354005
    Last edited by Bruce Fox; Aug 15th, 2005 at 10:18 PM.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: runing an addin macro

    The "parent", I assume, would be ATPVBAEN.XLA ... and Ive tried ATPVBAEN.XLA.Histogram
    ATPVBAEN.XLA!Histogram

    and even the full path to ATPVBAEN.XLA in the above examples ... nothing works so far

    Thanks again to all for the replies, though

  13. #13
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: runing an addin macro

    muddy..

    I have tested the code that I gave you on a sheet with some data, and it actually created a histogram..

    You are switching off the display alerts and then attempting to save a new workbook.. This will prompt for a saveas dialog..

    Change:

    VB Code:
    1. objXL.DisplayAlerts = False
    2. myWb.Save
    3. objXL.DisplayAlerts = True

    To:

    VB Code:
    1. objXL.DisplayAlerts = False
    2. myWb.SaveAs "Path and FileName"
    3. objXL.DisplayAlerts = True

    Something I missed off from the original code was the error trap.. so uncomment this line If i > objXL.Addins.Count Then GoTo NotAvailable and change the bottom to reflect this..

    VB Code:
    1. ExitProc:
    2.   Set mySheet = Nothing  'slight re-ordering here to help Excel close properly
    3.   Set myWb = Nothing
    4.   Set objAdd = Nothing
    5.   objXL.Quit
    6.   Set objXL = Nothing
    7.   Exit Sub
    8.  
    9. NotAvailable:
    10.   Msgbox "The Addin 'APTVBAEN.XLA' Is not present on this machine."
    11.   Resume ExitProc
    12. End Sub

    The Addin to be used needs to be present on the machine and also installed through the addin's menu option in Excel.. The Code I gave you checks to make sure that the addin is present and installs it if not installed.. Please Step through the code (F8) and tell which line presents the error.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: runing an addin macro

    OK, here is something interesting. If I open Excel, uncheck all the addins and then run your code it works (with the exception I had to change):
    VB Code:
    1. If Not objAdd.Installed Then
    2.     objXL.Addins.Add(objAdd.FullName).Installed = True
    3.   End If
    TO:
    VB Code:
    1. If Not objAdd.Installed Then
    2.     objAdd.Installed = True
    3.   End If
    NOTE that I took out if condition to see if this would work without unchecking the excel addins before running with NO LUCK. I need this to work even if the user already has the addins checked Ijnstalled) when he runs my app. Any ideas?
    Could this be an Office 2003 bug? Which version of XL are you running?
    Thanks for all the great help!

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: runing an addin macro

    OK, strange development, but I did find a fix thanks to all the help here. It turns out that (on my machine at least) I have to programatically uninstall and reinstall the adding before I can access its macros. The two lines that got me going are:
    VB Code:
    1. objAdd.Installed = False
    2.     objAdd.Installed = True

    The following is the complete snippet that works for me
    VB Code:
    1. Private Sub Command1_Click()
    2.   Dim objXL As Excel.Application
    3.   Dim objAdd As Excel.Addin
    4.   Dim myWb As Excel.Workbook
    5.   Dim mySheet As WorkSheet
    6.  
    7.   Set objXL = New Excel.Application  '(using an extra line for the New makes all code which uses objXL a bit faster)
    8.   objXL.Visible = True
    9.  
    10. 'get addin position
    11.   Dim i As Integer
    12.   For i = 1 To objXL.Addins.Count
    13.     If objXL.Addins(i).Name = "ATPVBAEN.XLA" Then
    14.         Set objAdd = objXL.Addins(i)
    15.         Exit For
    16.     End If
    17.   Next i
    18.   If i > objXL.Addins.Count Then GoTo NotAvailable
    19.  
    20.     objAdd.Installed = False
    21.     objAdd.Installed = True
    22.  
    23.   Set myWb = objXL.Workbooks.Add
    24.   Set mySheet = myWb.Sheets("Sheet1")
    25.   mySheet.Cells(4, 7).Value = 1
    26.   mySheet.Cells(5, 7).Value = 11
    27.   mySheet.Cells(6, 7).Value = 11
    28.   mySheet.Cells(7, 7).Value = 15
    29.   mySheet.Cells(8, 7).Value = 1
    30. objXL.DisplayAlerts = False
    31. myWb.Save
    32. objXL.DisplayAlerts = True
    33.   'I'm presuming that there is some data input at this point, other wise this will fail due to no data..
    34.   objXL.Run "Histogram", mySheet.Range("$G$4:$G$8"), _
    35.         "Hist", , False, True, True, False
    36.  
    37.  
    38. '(see note below)
    39. ExitProc:
    40.   Set mySheet = Nothing  'slight re-ordering here to help Excel close properly
    41.   Set myWb = Nothing
    42.   Set objAdd = Nothing
    43.   objXL.Quit
    44.   Set objXL = Nothing
    45.   Exit Sub
    46.  
    47. NotAvailable:
    48.   MsgBox "The Addin 'APTVBAEN.XLA' Is not present on this machine."
    49.   Resume ExitProc
    50.  
    51. End Sub

    Thanks everyone for all the great help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width