+ Reply to Thread
Results 1 to 9 of 9

Running Excel Macro in Batch File

Hybrid View

  1. #1
    Registered User
    Join Date
    10-22-2014
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2011
    Posts
    44

    Running Excel Macro in Batch File

    Hey All,

    I am ultimately trying to figure out how to open a .text file with excel, run a macro, overwrite and close the excel sheet.
    I have a batch file that copies one file and places it in another location as a .text file. The next step is to open the text file with excel, but I cannot figure out how to run the macro at that point.

    I am fairly new to the DOS command language and do not know whether or not I can write vba code in the batch file.

    Below is my batch command code.

    COPY f:\a\axys3\users\brandon\test.txt C:\Users\btoney\Desktop\IMRF\test.text /y 
    START "" "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" /e "C:\Users\btoney\Desktop\IMRF\test.txt"
    Any help would be much appreciated.

    Thank You,
    Brandon

  2. #2
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: Running Excel Macro in Batch File

    You need to create a .vbs script file which runs VB (v similar to VBA). Why don't you create a Workbook and code the file copy in that. You can then continue with the code you need to run.
    Frob first, tweak later

  3. #3
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: Running Excel Macro in Batch File

    Edit this in notepad and save as a .vbs file extension. You can continue with Excels' Object Model using the instance of xlApp you have created.

    Const Overwrite = True
    Const SaveChanges = True
    strSource = "f:\a\axys3\users\brandon\test.txt"
    strDest = "C:\Users\btoney\Desktop\IMRF\test.txt"
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    oFSO.CopyFile strSource, strDest , Overwrite
    set xlApp = CreateObject("Excel.Application")
    xlApp.visible = TRUE
    xlApp.Workbooks.Open strDest
    set wb = XlApp.Workbooks(1)
    wb.Activesheet.Range("A1:C4").Select
    xlApp.Selection.Font.Bold = True
    wb.Close SaveChanges
    xlApp.Quit
    Added a little more code so you can see how to edit the sheet, save, close and quit the app. The font.Bold will have no effect if you save as a .txt file though, it's just there so you can see how to use the object model.
    Last edited by Neil_; 08-16-2016 at 01:59 PM.

  4. #4
    Registered User
    Join Date
    10-22-2014
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2011
    Posts
    44

    Re: Running Excel Macro in Batch File

    Thank you Neil that was a lot of help.

    Does "xlApp" have specific code you need to use?

    I am getting an "expected statement error" with the additional code I tried to use.

    Const Overwrite = True
    strSource = "f:\a\axys3\users\brandon\test.txt"
    strDest = "C:\Users\btoney\Desktop\IMRF\test.txt"
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    oFSO.CopyFile strSource, strDest , Overwrite
    set xlApp = CreateObject("Excel.Application")
    xlApp.visible = TRUE
    xlApp.Workbooks.Open strDest
    set wb = XlApp.Workbooks(1)
    	wb.Activesheet.Cells.Replace What:="causcash", Replacement:="USD Curncy", LookAt:=xlPart _
     	, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
     	ReplaceFormat:=False

  5. #5
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: Running Excel Macro in Batch File

    Hi,
    When using vb to control Excel, enumerations (i.e. LookAt:=) and constants (i.e. xlPart) are outside the scope of the vb script, so you must be specific and use the function 'as is'. xlPart and xlByRows are actually integers, so vb must use integers when passing the code to the object model. You can declare them as constants at the beginning for readability.

    Const Overwrite = True
    Const xlPart = 2
    Const xlByRows = 1
    
    strSource = "f:\a\axys3\users\brandon\test.txt"
    strDest = "C:\Users\btoney\Desktop\IMRF\test.txt"
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    oFSO.CopyFile strSource, strDest , Overwrite
    set xlApp = CreateObject("Excel.Application")
    xlApp.visible = TRUE
    xlApp.Workbooks.Open strDest
    set wb = XlApp.Workbooks(1)
    wb.Activesheet.Cells.Replace "causcash", "USD Curncy", xlPart, xlByRows, False, False, False, False
    Values for constants can be found here

    https://msdn.microsoft.com/en-us/lib.../ff839040.aspx
    Last edited by Neil_; 08-17-2016 at 09:35 AM.

  6. #6
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: Running Excel Macro in Batch File

    If all you are wanting to do is a text replace, you do not need Excel.

    untested
    
    strSource = "f:\a\axys3\users\brandon\test.txt"
    strDest = "C:\Users\btoney\Desktop\IMRF\test.txt"
    strFind = "causcash"
    strReplace = "USD Curncy"
    Set inputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strSource, 1)
    strInputFile = inputFile.ReadAll
    inputFile.Close
    Set inputFile = Nothing
    Set outputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDest, 2, True)
    outputFile.Write Replace(strInputFile, strFind, strReplace)
    outputFile.Close
    Set outputFile = Nothing

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Running Excel Macro in Batch File

    Thanks Mr. Neil
    As for post #3 .. Where the created workbook saved?
    How can I specify the path where I need to save?
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  8. #8
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: Running Excel Macro in Batch File

    @YasserKhalil

    It defaulted as the file that was opened, so the save path would be the same

    strDest = "C:\Users\btoney\Desktop\IMRF\test.txt"


    If you wanted to save the text file as an Excel document before closing:-

      
    wb.SaveAs  "C:\Test.xlsx"
    wb.Close
    Last edited by Neil_; 08-17-2016 at 10:31 AM.

  9. #9
    Registered User
    Join Date
    10-22-2014
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2011
    Posts
    44

    Re: Running Excel Macro in Batch File

    Thank you for your help Neil. I'm going to try and work through re-creating my old macro using vb. I'm marking this post as "solved", but hopefully I can reach back out to you if anything comes up.

    Thanks again for your help and providing the enumeration page.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need macro that opens .txt file in excel, run macros and save file in batch
    By khalidness in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-11-2014, 11:00 AM
  2. Batch file to run excel macro
    By shotta in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-31-2013, 12:48 PM
  3. [SOLVED] Need help batch running an excel macro on .csv files and saving back to the same .csv
    By OutKlast in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-31-2013, 09:16 PM
  4. Getting an exception while running a batch file from vb script
    By sharabh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-25-2012, 01:02 AM
  5. Running batch file
    By walduxas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2009, 08:07 AM
  6. Running a batch file from Excel
    By tryer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-21-2008, 09:47 AM
  7. Run Excel Macro through Batch file
    By LJi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-03-2005, 02:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1