Consulting

Results 1 to 18 of 18

Thread: How to use VBA to write a bat file and execute it?

  1. #1

    How to use VBA to write a bat file and execute it?

    Hi all,

    If I need to edit the bat.file oftenly, just wonder if it will be more convenient to write a marco helping this...

    The contents of the bat.file are sentences like:
    Call XXXX
    Call YYYY

    How to save it as a bat.file and execute it automatically?

    Thanks in advance

  2. #2
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by snoopies
    Hi all,

    If I need to edit the bat.file oftenly, just wonder if it will be more convenient to write a marco helping this...

    The contents of the bat.file are sentences like:
    Call XXXX
    Call YYYY

    How to save it as a bat.file and execute it automatically?

    Thanks in advance
    If you are going to execute the bat file automatically, why not just do the whole work in VBA? That is, forget the .bat file.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Quote Originally Posted by xld
    If you are going to execute the bat file automatically, why not just do the whole work in VBA? That is, forget the .bat file.
    VBS is another alternative.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by xCav8r
    VBS is another alternative.
    Indeed.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Hi all,

    Please allow me to ask another stupid question..

    I have read the article suugested above, it's really useful...Thank you

    I have successfully created a bat file in excel.. but I can't invoke the batch file... e.g.. if the file name is XXX.bat , which is saved in C:\

    Shell ("C:\XXX.bat"), vbHide '', vbMinimizedNoFocus <--- Is it Correct? It shows no response... but when I go to C drive and click XXX.bat manually...It works!

    What's missing?

    Thanks a lot!

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    The syntax is

    Shell(pathname[,windowstyle])

    so you should use

    Shell  "C:\XXX.bat", vbHide
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Thx for reply

    if I use...

    set exportpath="C:\XXX.bat"
    Shell (exportpath), vbHide
    Anything's wrong..?

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by snoopies
    Thx for reply

    if I use...

    set exportpath="C:\XXX.bat"
    Shell (exportpath), vbHide

    Anything's wrong..?
    exportpath = "C:\XXX.bat"
    Shell exportpath, vbHide
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    exportpath = "C:\XXX.bat"
    Shell exportpath, vbHide
    It's still not working.... pls help..

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by snoopies
    exportpath = "C:\XXX.bat"
    Shell exportpath, vbHide

    It's still not working.... pls help..
    Post the bat file code and let's see.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    okay..pls take a look of this...

    Sub shell_bat()
    Dim ExportPath As String
    Dim TempFileName As String
    Dim iFileNum As Long
    ' Create Batch Program
    Set wsActive = ActiveSheet
    With wsActive
    ExportPath = "C:\studies\"
    TempFileName = ExportPath & Trim(.Name) & ".bat"
    iFileNum = FreeFile
    Open sTempFileName For Append As #iFileNum
    Print #iFileNum, "@Echo off"
    Print #iFileNum, "CLS"
    Print #iFileNum, "If exist dic.log del dic.log"
    Print #iFileNum, "If exist err.log del err.log"
    Print #iFileNum, "call dic.bat" & " " & Trim(.Name) & " " & "dic32" & " " & "c" & " " & "studies" & " " & "c" _& " " & "studies"
    End With
    Close #iFileNum
    'Invoke Batch Program"
    Shell sTempFileName, vbHide
    End sub
    Last edited by Airborne; 07-19-2005 at 08:07 PM. Reason: Wrap [VBA] tags

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Snoopies,

    SET OPTION EXPLICIT IN YOUR CODE, IT WILL SAVE YOU PROBLEMS.


    Sub shell_bat()
    Dim ExportPath As String
    Dim iFileNum As Long
    Dim wsActive As Worksheet
    Dim sTempFileName As String
    ' Create Batch Program
    Set wsActive = ActiveSheet
    With wsActive
        ExportPath = "C:\"
        sTempFileName = ExportPath & Trim(.Name) & ".bat"
        iFileNum = FreeFile
        Open sTempFileName For Output As #iFileNum
        Print #iFileNum, "@Echo off"
        Print #iFileNum, "CLS"
        Print #iFileNum, "If exist dic.log del dic.log"
        Print #iFileNum, "If exist err.log del err.log"
        Print #iFileNum, "call dic.bat" & " " & Trim(.Name) & " " & "dic32" & " " & "c" & " " & "studies" & " " & "c" _
        & " " & "studies"
    End With
    Close #iFileNum
    'Invoke Batch Program"
    Shell sTempFileName, vbHide
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    still not working......

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by snoopies
    still not working......
    In what way? It worked for me.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    hello, my problem..

    I can create a new bat file called XXX.bat sucessfully, being put in c:\studies
    however, if it works properly, if I invoke XXX.bat, it will call another bat file called dic.bat.. that's my problem.. dic.bat can not be invoked automatically...
    But when I open folder c:\studies, find XXX.bat, click and execute it, the dic.dat can work properly.. It's very strange..

  17. #17
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Here try this

    With wsActive
            ExportPath = "C:\"
            sTempFileName = ExportPath & Trim(.Name) & ".bat"
            iFileNum = FreeFile
            Open sTempFileName For Output As #iFileNum
            Print #iFileNum, "@Echo off"
            Print #iFileNum, "CLS"
            Print #iFileNum, "c:"
            Print #iFileNum, "cd\"
            Print #iFileNum, "If exist dic.log del dic.log"
            Print #iFileNum, "If exist err.log del err.log"
    Print #iFileNum, "call dic.bat" & " " & Trim(.Name) & " " & "dic32" & " " & "c" & " " & "studies" & " " & "c" _
            & " " & "studies"
        End With


    The reason it is not executing.... Your current directory is not set. I am forcing your current directory to be root of C, by adding to your batch file (change to suit your needs).

  18. #18
    YES, you are right!

    It works properly now..

    Thank you so much for your help!
    Thanks for teaching this..

Posting Permissions

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