Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

command line in Word 1

Status
Not open for further replies.

HowardHammerman

Instructor
Oct 11, 2001
640
US
Is it possible to create a *.bat file that calls word, opens a particular *.docx file, and runs mailmerge with the saved settings?



Howard Hammerman,
Crystal Training and Crystal Material
On-site classes and one-on-one coaching
Low-cost telephone/email support
 
Well sort of. You could do it in javascript or vbscript and run it using cscript in a batch file.

The easiest thing to do is to record a macro which does the mailmerge, have a look at it and convert it to vbscript which is basically the same as VBA without types.

Then add in a front bit to open the word object and open the document and an end bit to close the document and the word object.

A final check after you've run your batch file is to check the task manager to see if winword is still running. If it is, your script did not terminate properly.
 
Yikes! That sounds like a challange. I have never written VB or Java. I do know how to create a macro. Can you post some code?
Is this compiled?

Howard Hammerman,
Crystal Training and Crystal Material
On-site classes and one-on-one coaching
Low-cost telephone/email support
 
It is not vb or java: it is vbscript or javascript. There is a lot of difference. You don't need to compile it and you can even run it on a Win98 machine.

Example of creating a document and writing a couple of paragraps
Code:
Option Explicit
dim wordApp
set wordApp = WScript.CreateObject ("Word.Application")
' Start here
WScript.Echo "Word created"
with wordApp
   .Visible = TRUE
   .Documents.Add ()
   .Selection.Typetext "This is the first paragraph"
   .Selection.TypeParagraph
   .Selection.Typetext "This is the second paragraph"
   WScript.Echo "saving"
   '.Printout ()
   .ActiveDocument.SaveAs "C:\Documents and Settings\All Users\Documents\scriptrep.doc"
   ' Give word 1 second to catch up
   WScript.Sleep 1000
   WScript.Echo "closing"
   .ActiveDocument.Close()
   WScript.Echo "quit"
   .Quit ()
end with
For more examples look at
If you are not sure what to do, record a macro and have a look at what has been generated.
 
Thank you. That is a good lead. I guess I can write the script in notepad. What extension do I use? VBS?


Howard Hammerman,
Crystal Training and Crystal Material
On-site classes and one-on-one coaching
Low-cost telephone/email support
 
Yes .vbs

Say you called it twopara.vbs To run it from the cmd prompt

cscript twopara.vbs

Alternatively you can just double click on it in explorer. I prefer the cmd prompt way because it tells you when something is not right. If it does go wrong, make sure that you kill off all instances of winword running in the background otherwise you could end up with all sorts of strange happenings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top