Macro gets disabled if called using a vb script

前端 未结 2 600
Happy的楠姐
Happy的楠姐 2021-01-13 03:22

I am trying to call my excel macro using vbs. Here is a snippet of my code.

Set objExcel = CreateObject(\"Excel.Application\")
Set objWorkbook = objExcel.W         


        
相关标签:
2条回答
  • 2021-01-13 03:55

    To add to Siddhart's answer - you can load the addins you require in a VBScript like this:

    objExcel.RegisterXLL("analys32.xll") 'For XLL addins
    objExcel.Workbooks.Open(objExcel.LibraryPath & "\analysis\atpvbaen.xla") 'For standard XLA addins
    objExcel.Workbooks.Open("C:\Program Files\MyAddins\MyAddin.xla") 'for custom XLA addins
    
    0 讨论(0)
  • 2021-01-13 04:05

    Try this

    Dim objExcel, objWorkbook 
    
    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\Test_PO.xls")
    objExcel.Visible = True
    objExcel.Run "Data_Analysis"
    objWorkbook.Close
    objExcel.Quit
    
    Set objWorkbook = Nothing
    Set objExcel = Nothing
    
    WScript.Echo "Finished."
    WScript.Quit
    

    EDIT

    If the macro is in a module then the above will help. If the macro is in a sheet say, Sheet1 then replace the line

    objExcel.Run "Data_Analysis"
    

    with

    objExcel.Run "sheet1.Data_Analysis"
    

    FOLLOWUP

    Try this code.

    Dim objExcel, objWorkbook, ad, FilePath
    
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = True
    
    For Each ad In objExcel.AddIns
        If ad.Name = "Converteam.xla" Then
            FilePath = ad.Path & "\Converteam.xla"
            Exit For
        End If
    Next
    
    objExcel.Workbooks.Open (FilePath)
    
    Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\Test_PO.xls")
    
    objExcel.Run "Data_Analysis_Converteam"
    objWorkbook.Close
    objExcel.Quit
    
    Set objWorkbook = Nothing
    Set objExcel = Nothing
    
    WScript.Echo "Finished."
    WScript.Quit
    

    EXPLANATION:

    When you use CreateObject, the Add-Ins are not installed by default. Please see this link.

    Topic: Add-ins do not load when using the CreateObject command in Excel

    Link: http://support.microsoft.com/kb/213489/

    You have to load the Add-In and then call the relevant macro. Also the name of your macro is not Data_Analysis but Data_Analysis_Converteam

    HTH

    0 讨论(0)
提交回复
热议问题