Results 1 to 11 of 11

Thread: [RESOLVED] how to run VBA code from batch file

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Resolved [RESOLVED] how to run VBA code from batch file

    To run the Excel VBA code, the only way I know so far is to open the Excel file, click Alt+F8, then click 'run' button, or use Alt+F11, and run from there.

    Is there any way to call and run it from batch file in Windows or cron in Linux?

    Thanks.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: how to run VBA code from batch file

    in windows you can run vb like code using the windows scripting host
    any textfile saved with .vbs extension will run the code within, when double clicked, from this you can create objects to read /write file or registry, automate office applications, or other activex dlls

    many standard vb functions and methods will work, but some have to be coded differently
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to run VBA code from batch file

    How about taking it out of VBA and putting it into a language that would permit you to create a compiled .Exe?

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Re: how to run VBA code from batch file

    I just tried to put my VBA code from Excel Module to an external file with extension as .vbs. But when I double click it, it pops up an error: Expected end of statement, Code: 800A0401. It says it is a compilation error.

    It runs well inside Excel file. How come it is a compilation error when saved to an external file.

    How should I solve this?
    Last edited by scorpioy; Oct 29th, 2008 at 08:56 AM.

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: how to run VBA code from batch file

    I need to see your code...

    I could be wrong but I feel you might be early binding. If yes, then switch to late binding...

    Can I see your code?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Re: how to run VBA code from batch file

    My code is relatively simple. It's reading some input files and produce some output files.

    Code:
    Dim inputBook As Excel.Workbook
    Dim outputBook As Excel.Workbook
    
    Dim inputApp, outputApp As Excel.Application
    Set inputApp = New Excel.Application
    
    ...
    
    Set inputBook = inputApp.Workbooks.Open(inputFileName)
    Is there some problem?

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: how to run VBA code from batch file

    Yeah I was right... you are early binding... you need to use latebinding

    for example

    Code:
    Dim inputBook As Object
    Dim outputBook As Object
    
    Dim inputApp, outputApp As Object
    Set inputApp = CreateObject("Excel.Application") 
    .
    .
    . And so on..
    Check put Si's tutorial here on late binding...

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Re: how to run VBA code from batch file

    Thanks for the help.
    I actually have another concern. My code is running against some configuration value I specifed inside the Excelf file itself. So when I copy the code out. I still want my code to read the configuration values. So is there a way to ask Excel file to run at all?

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: how to run VBA code from batch file

    I still want my code to read the configuration values.
    Can you elaborate on this?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Re: how to run VBA code from batch file

    My program is reading and writing files. So I specified file paths and names in the Excel sheet, and I have VBA code in Modules to run on it.

    During run time, my code will read in those file info from the Excel sheet itself, and execute accordingly.

    It's like I put some constant values in the Excel sheet (and read them during execution), instead of defining constant inside the code.

    Is this clear enough?

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: how to run VBA code from batch file

    once you create and object instance of excel you can open workbooks and basically do anything that you can do in excel code (with some differences), see below,
    if you want to read or write to text files you need to use FSO as vb file io does not work in vb scripting

    variables in vbs should not be typed
    vb Code:
    1. dim oxl, owb, osht
    2. set oxl = createobject("excel.application")
    3. set owb = oxl.workbooks.open(somepath&filename)
    4. set osht = owb.sheets("mysheet")
    5. msgbox osht.range("a1").value

    you can also create an object of a specific workbook with out the excel object
    set owb = createobject("somepathandfilename.xls")
    or if the workbook is already open use
    set owb = getobject("somepathandfilename.xls")
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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