none
Batch script to open multiple excel and doc files RRS feed

  • Question

  • Hi Everyone,

    Could anybody please provide a batch script to open multiple excel and doc files at a time.

    Appreciate ur quick response.

    Regards,
    Monday, February 8, 2010 7:50 AM

Answers

  • You have several scripting choices within Windows. At a basic level you've got "batch files" - which run a series of command interpreter commands, and have a file extension of .BAT or .CMD. If you just wanted to open a list of Word documents, then assuming that the path to Word is correct (something like this below) you could list these in a file with a .BAT extension and run this (e.g. double click on it):

    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File1.DOC"
    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File2.DOC"
    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File3.DOC"
    "C:\Program Files\Microsoft Office\Office\Excel.exe" "C:\Documents and Settings\All Users\Desktop\File1.XLS"
    "C:\Program Files\Microsoft Office\Office\Excel.exe" "C:\Documents and Settings\All Users\Desktop\File2.XLS"

    Another script language is VBScript, which was the example I gave. VBScript is available on most Windows platforms and can be run from a command prompt or within Windows. If you save the text I gave you above to a file with a .VBS extension you can double-click to run it (Windows) or open a command prompt and type CSCRIPT MyVBScript.VBS (assuming that's the name of the your .VBS file).
    Other script languages are available to achieve the same thing, including Powershell, however you'd need to have Powershell available on your PC.
    • Marked as answer by IamMred Tuesday, February 23, 2010 5:07 AM
    Wednesday, February 10, 2010 10:51 AM

All replies

  • strComputer="."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colFileList = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_Directory.Name='C:\test'} Where ResultClass = CIM_DataFile")
    
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = TRUE
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    For Each objFile In colFileList
        If objFile.Extension = "xls" Or objFile.Extension = "xlsx" Then
            objExcel.Workbooks.Open objFile.Name
        ElseIf objFile.Extension = "doc" or objFile.Extension = "docx" Then
            objWord.Documents.Open objFile.Name
        End If
    Next
    This assumes that all your spreadsheets and documents are in one folder, C:\test. Otherwise just adapt this for specific paths.

    Monday, February 8, 2010 1:46 PM
  • Is this a batch script of power shell script ?
    Wednesday, February 10, 2010 4:43 AM
  • You have several scripting choices within Windows. At a basic level you've got "batch files" - which run a series of command interpreter commands, and have a file extension of .BAT or .CMD. If you just wanted to open a list of Word documents, then assuming that the path to Word is correct (something like this below) you could list these in a file with a .BAT extension and run this (e.g. double click on it):

    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File1.DOC"
    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File2.DOC"
    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File3.DOC"
    "C:\Program Files\Microsoft Office\Office\Excel.exe" "C:\Documents and Settings\All Users\Desktop\File1.XLS"
    "C:\Program Files\Microsoft Office\Office\Excel.exe" "C:\Documents and Settings\All Users\Desktop\File2.XLS"

    Another script language is VBScript, which was the example I gave. VBScript is available on most Windows platforms and can be run from a command prompt or within Windows. If you save the text I gave you above to a file with a .VBS extension you can double-click to run it (Windows) or open a command prompt and type CSCRIPT MyVBScript.VBS (assuming that's the name of the your .VBS file).
    Other script languages are available to achieve the same thing, including Powershell, however you'd need to have Powershell available on your PC.
    • Marked as answer by IamMred Tuesday, February 23, 2010 5:07 AM
    Wednesday, February 10, 2010 10:51 AM
  • Hey

    Sorry responding lately, i was out of station.. where i could not browse.

    I have used the below one as per requirement and saved it as a bat file and executed..

    The result : it is executing only the file first one not the rest, script is going to the second file to open it.

    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File1.DOC"
    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File2.DOC"
    "C:\Program Files\Microsoft Office\Office\Winword.exe" "C:\Documents and Settings\All Users\Desktop\File3.DOC"
    "C:\Program Files\Microsoft Office\Office\Excel.exe" "C:\Documents and Settings\All Users\Desktop\File1.XLS"
    "C:\Program Files\Microsoft Office\Office\Excel.exe" "C:\Documents and Settings\All Users\Desktop\File2.XLS"

    Help me with this

    Regards,


    Tuesday, March 16, 2010 12:01 PM
  • You're right - it opens one at a time and won't progress to the next until the current window is open. Very odd. I've even tried it with a CALL command as well as trying to launch separate batch files and it doesn't like it.
    You're going to have to script it I'm afraid. Either try the VBScript I posted above - save the contents to a file with a .VBS extension, create a folder called C:\TEST and put all the Word and Excel documents into it and then double click to run the VBS file, or a slight modification to the VBScript file passing a list of documents would be:

    ' Open these Word documents..
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    objWord.Documents.Open "C:\Documents and Settings\All Users\Desktop\File1.DOC"
    objWord.Documents.Open "C:\Documents and Settings\All Users\Desktop\File2.DOC"
    objWord.Documents.Open "C:\Documents and Settings\All Users\Desktop\File3.DOC"
    'Open these Excel speadsheets
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = TRUE
    objExcel.Workbooks.Open "C:\Documents and Settings\All Users\Desktop\File1.XLS"
    objExcel.Workbooks.Open "C:\Documents and Settings\All Users\Desktop\File2.XLS"

     

    Wednesday, March 17, 2010 1:25 PM
  • The START utility will fix the problem.  Also, since the XLS and DOC extensions are associated with the programs that open them, it's not actually necessary to name the program explicitly.  So, something like this should work ...

      for %%a in (File1.doc File2.doc File3.doc File4.xls File5.xls) do (
        start "" "C:\Documents and Settings\All Users\Desktop\%%a"
      )

    This will start one after the other without waiting for the preceding application to close.  Note that first set of double quotes.  They're needed to keep the START utility from using the file name as if it were a title.  See the command line help for more syntax information (START/? at a command prompt).


    Tom Lavedas
    • Edited by Tom LavedasModerator Wednesday, March 17, 2010 1:47 PM to fix a second typo
    • Proposed as answer by The_Cat Wednesday, March 17, 2010 2:40 PM
    Wednesday, March 17, 2010 1:44 PM
    Moderator
  • thanks Cats and Tom
    Sunday, March 28, 2010 9:17 PM
  • I have a script that opens two excel files and has both running simultaneously. I found the only way to make this work is as follows:

    CD (path)

    Start "" ""file1.xls"

    "file2.xls"

    This assumes both files are located in the same directory. If not, you wil need to perform another CD command after issuing the start command.


    • Edited by JSavage Tuesday, October 29, 2013 7:10 PM
    Tuesday, October 29, 2013 7:10 PM
  • t

    This script worked.  Now I would like to save all files as .txt files and close them.  I tried .bat file code but it didn't work. 

    Also, can I Call a VB script from a Batch file?

    thx !!

    Thursday, February 13, 2014 3:37 PM
  • This works. It opens all the files.  Can it save all the files using a loop, too?  Thz
    Thursday, March 27, 2014 5:11 PM
  • That did it.

    Thanks a lot.

    --

    Rawat

    Thursday, March 16, 2017 5:39 AM