How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Richard Your Own Question

Richard
Richard, Software Specialist
Category: Microsoft Office
Satisfied Customers: 16842
Experience:  Over 15 year experience resolving Microsoft Office Issues
32989067
Type Your Microsoft Office Question Here...
Richard is online now
A new question is answered every 9 seconds

I have a developed an Excel Spreadsheet which I would like

Resolved Question:

I have a developed an Excel Spreadsheet which I would like to Open and Automatically Run a macro. I do not want the macro to run when the spreadsheet is opened. I want to only have it run via a bat file. I would like to have the *.bat file created because I cannot figure this piece out.
Submitted: 1 year ago.
Category: Microsoft Office
Expert:  Richard replied 1 year ago.
Thank you for your question, my name is Richard.

What you need to do is the following from a VBS file.

First, the Macro needs to be in a Module, not the sheet.

Then in your VBS file you use

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("test.xlsx")
objExcel.Application.Run "test.xlsx!MyMacro"

Here my example assumes your file is test.xlsx in the same folder as the VBS file and the Macro is called MyMacro (in a module)

It then opens the Excel and runs the Macro

Then to run the VBS from a batch file, you put the batch file in the same folder as the Excel and VBS and put this in it

@echo off
%WINDIR%\SysWOW64\cmd.exe
cscript My.vbs

Assuming the vbs file is called My.vbs

You cannot pass a value to the Batch though, it needs to be static.

If you have any difficulties at all, please do not hesitate to let me know so I can assist you further

Thank you
Customer: replied 1 year ago.

So did you say that the path/filename must be static in the My.vbs code?


I will give this a try and get back with you.

Expert:  Richard replied 1 year ago.
You can pass the path name to the VBS, but not into the Batch file, so the Batch file is as

@echo off
%WINDIR%\SysWOW64\cmd.exe
cscript My.vbs "c:\path to xlsx file\xlsx file

then in the VBS

Set File = FSO.OpenTextFile(WScript.Arguments(0) &"\test.txt", 2, True)

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(file)
objExcel.Application.Run file & "!MyMacro"

That allows you to pass the value into the VBS

Customer: replied 1 year ago.

OK. I am ready to tackle this.


 


In the


Set File = FSO.OpenTextFile(WScript.Arguments(0) &"\test.txt", 2, True)


 


what is \test.txt??

Expert:  Richard replied 1 year ago.
That is the path name, in my example it is \text.txt

You replace it with your file name

eg: \test.xlsx

and make sure the VBS file is in the same folder as the Excel file.

Customer: replied 1 year ago.

Still not clear.


 


Would this be the correct bat?


 


‘Using for example to pass to vbs file Path=C:\documents, Filename=filename.xls


 


@echo off
%WINDIR%\SysWOW64\cmd.exe
cscript My.vbs "c:\documents\filename.xls”


 


What should the vbs be in this example

Expert:  Richard replied 1 year ago.
yes, and you still use the same code in the VBS script

Set File = FSO.OpenTextFile(WScript.Arguments(0), 2, True)

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(file)

objExcel.Application.Run file & "!MyMacro"

Remember the line

objExcel.Application.Run file & "!MyMacro"

Is for running your Macro in the Excel file.


Customer: replied 1 year ago.

So if I understand correctly, the test.txt was to be used if I do not pass the path\filename from the bat file since it was removed.


 


I am trying the real thing now. This has a long path and long file name, but these are the ones I am testing. Doesn't run. I leaves me in cmd prompt with c:\users\farram1\Desktop. Do you know what might be wrong??


 


‘Using for example to pass to vbs file


' Path=c:\Users\farram1\Desktop\IZ Sediment\, ' 'Filename=HCAHPS8.16.2013.xlsm


 


@echo off
%WINDIR%\SysWOW64\cmd.exe
cscript C:\Users\farram1\Desktop\IZ Sediment\IZMacroLaunch.vbs "c:\Users\farram1\Desktop\IZ Sediment\HCAHPS8.16.2013.xlsm”

Expert:  Richard replied 1 year ago.
No, the test.txt was just an example from me, if you were only passing the folder and not the file eg: c:\documents

But you leave it out, it was just my example.


This

C:\Users\farram1\Desktop\IZ Sediment\IZMacroLaunch.vbs

Needs to be in " "

So


@echo off
%WINDIR%\SysWOW64\cmd.exe
cscript "C:\Users\farram1\Desktop\IZ Sediment\IZMacroLaunch.vbs" "c:\Users\farram1\Desktop\IZ Sediment\HCAHPS8.16.2013.xlsm”

Let me know the results please

Customer: replied 1 year ago.

I added the "" and it still does not do anything. Just see the cmd with directory name. I even tried running everything from the path. use this code. Ran from in the path, and it just shows the directory on the screen from the cmd.


 


@echo off
%WINDIR%\SysWOW64\cmd.exe
cscript "IZMacroLaunch.vbs" "IZ Integrated HCAHPS8.16.2013.xlsm”

Expert:  Richard replied 1 year ago.
It will be an issue with your paths

Click Start, type in CMD and press enter

Then drag the BAT file into the black window and press enter

Tell me the error it is saying please

Customer: replied 1 year ago.

I had to type this in. Is there any way to click and drag the code to the cmd?


 


The message said cannot find script file

Expert:  Richard replied 1 year ago.
You can drag the Batch file itself into the black screen

It then puts the path in there.

If it says it cannot find the script file, it means the path is not correct.

To make it easier. Make a folder directly under C drive Marilyn.

And put all involved files there

Call the folder JA as an example

Then make all your paths c:\JA (then the file name).

this way you can be sure you are getting the paths correct to the files.

Let me know the results please

Thank you
Customer: replied 1 year ago.

I created a c:\Marilyn path and placed all files in it plus changed the bat to match. Just sits at c:\Marilyn and doesn't do anything. Same as before. I think I am going to create a text.xls file that will simply write a worksheet to a pdf file in a macro. Then, I could at least send you this to see if it doen'st work. No clue why this is not running.


 

Expert:  Richard replied 1 year ago.
Can I log on and look please on the connected computer?

It is easier.

Go to Click Me

Click START MEETING

It will download a program to your computer

Run the program by double clicking it, and it will give you a set of 9 numbers

Give me the numbers at the top of the screen (eg: 456-789-123) in your message to me here please

Thank you
Customer: replied 1 year ago.

I ran this from c:\Marilyn but if you create a path and verify this works then we would know.


 


Here is a test file that just writes a pdf file.


http://wikisend.com/download/569662/test.xlsm


 


bat file


 


@echo off
%WINDIR%\SysWOW64\cmd.exe
cscript "c:\Marilyn\IZMacroLaunch.vbs" "c:\marilyn\test.xlsm"


 


 


 


vbs file


 


 


Set File = FSO.OpenTextFile(WScript.Arguments(0), 2, True)


Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(file)


objExcel.Application.Run file & "!Macrotest"


 


It just comes back with >c:\Marilyn cmd mode.


Can you download this test file. Create a path, and then try this code to validate it works. I still think something is off.


 

Expert:  Richard replied 1 year ago.
We will do it this way

I changed it a little bit.

Download these three files and put direct in C in a folder called testt

so c:\testt

You download them from Wikisend under Files Ids

918768
590094
479482

Run the Batch and Let me know the results please

Thank you
Customer: replied 1 year ago.

I did get this to run ok.


 


Does this mean, we just cannot pass the path\filename from the bat file to the vba file?

Expert:  Richard replied 1 year ago.
yes we can

I just wanted to make sure it worked first

I have updated these two files

885358

532204

Let me know if you have any difficulties please

Customer: replied 1 year ago.

I see the empty msgbox open, which I deleted because I don't need this.


 


This new way does not open and run the macro in my test spreadsheet.


 


So you have to type the path\filename anyway to run a macro from bat right?

Expert:  Richard replied 1 year ago.
correct, which is why I said initially it would be static, you are just putting the name in the Batch, instead of the VBS

Can you use the initial files I provided?

Customer: replied 1 year ago.

In the new VBS file that you sent that we are trying to pass the filename,


don't I still end up typing the filename into this line of code in the vbs file?


xl.Application.run "'c:\Marilyn\test.xlsm'!macrotest"


 


I need to take a couple steps back and see what is off because the programs that were working a minute ago are not. So let me work with these a little while.

Expert:  Richard replied 1 year ago.
ok, download the files again

918768
590094
479482


and use these as they work on your system please.

Customer: replied 1 year ago.

I backed up and still have the code working with the filename not passed in bat, but in the vbs using this code.


 


 


xl.Application.Workbooks.Open "C:\Marilyn\test.xlsm"


 


 


However, still do not have the one working where you pass the filename via bat to vbs. xl.Application.Workbooks.Open file. If you have to type the path/filename into the RunMacro row in the vbs file, this defeats my purpose for wanting to pass it from the bat file.


 

Expert:  Richard replied 1 year ago.
ok, then in the VBS add at the top

Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.OpenTextFile(WScript.Arguments(0), 2, True)

Then put File in the place of the path

xl.Application.Workbooks.Open File

Then in the Batch file

@echo off

cscript "c:\testt\test.vbs" "c:\testt\test.xlsx"

If the paths are correct, that will work.

Richard, Software Specialist
Category: Microsoft Office
Satisfied Customers: 16842
Experience: Over 15 year experience resolving Microsoft Office Issues
Richard and 5 other Microsoft Office Specialists are ready to help you
Customer: replied 1 year ago.

I have a piggyback to this question, and will pay via a bonus.


 


I have the text.xlsm macro that I am running using some SendKeys.


 


i.e. SendKeys "%{A}". For some reason when I run a macro external as from bat.vbs combination. Any Sendkeys in the macro do not run.


 


This is important to my macro I have developed because it was the only way I could get accurate results. No Sendkeys invoke when I run the macro externally. I have a test.xlsm, bat, vbs of what I am using.


 


The macro will run fine from within the xlsm file if you open and run.

Expert:  Richard replied 1 year ago.
Hi Marilyn,

Thank you for this, but I need it to be a new question sorry.

It is per Site Policy.

You can make one though here

http://www.justanswer.com/computer/expert-richieme/

And I would be happy to assist you further.

Thank you

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Last | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
 
 
 

Meet The Experts:

 
 
 
  • jstinehelfer

    Information Systems Manager

    Satisfied Customers:

    36
    A+ Comptia Certified computer repair
< Last | Next >
  • http://ww2.justanswer.com/uploads/JS/jstinehelfer/2012-6-6_215146_Pictureofme1.64x64.png jstinehelfer's Avatar

    jstinehelfer

    Information Systems Manager

    Satisfied Customers:

    36
    A+ Comptia Certified computer repair
  • http://ww2.justanswer.com/uploads/JA/JasonJames122/2011-12-16_135647_jasonjamesheadshotweb.64x64.jpg JasonJames122's Avatar

    JasonJames122

    Computer Enthusiast

    Satisfied Customers:

    0
    I manage all information systems (network of Windows PC's and servers with Microsoft Office products) for a small business
  • http://ww2.justanswer.com/uploads/JE/jessmagz/2012-6-6_18129_jm.64x64.jpg Jess M.'s Avatar

    Jess M.

    Computer Support Specialist

    Satisfied Customers:

    301
    Computer Software Specialist for more than 10 years
  • http://ww2.justanswer.com/uploads/CH/chutz747/2011-11-6_14231_me.64x64.jpg The-PC-Guy's Avatar

    The-PC-Guy

    Computer Manager

    Satisfied Customers:

    274
    20 years experience providing remote computer support
  • http://ww2.justanswer.com/uploads/SP/spherrod/2012-6-6_174244_1000852.64x64.JPG Steve Herrod's Avatar

    Steve Herrod

    Computer Support Specialist

    Satisfied Customers:

    125
    Trained in Microsoft Office from 2000 through to latest version 2010.
  • http://ww2.justanswer.com/uploads/SE/Seashore2011/2011-12-6_33418_111205223015.64x64.jpg Chris L.'s Avatar

    Chris L.

    Support Specialist

    Satisfied Customers:

    124
    Certified Expert with over 10 years experience.
  • http://ww2.justanswer.com/uploads/KR/krkljatorm/2012-6-24_153417_ITMiro.64x64.jpg IT Miro's Avatar

    IT Miro

    Computer Scientist

    Satisfied Customers:

    97
    Bachelor's Degree in Information Technology, Microsoft Certified Professional
 
 
 
Chat Now With A Microsoft Office Technician
Richard
Richard
IT Manager
902 Satisfied Customers
Over 15 year experience resolving Microsoft Office Issues