Solved

send outout from a batch file to MS-word

Posted on 2004-10-09
11
512 Views
Last Modified: 2008-02-01
I have a batch file what checks some values in a text file.
I want to put the results direct to a MSWord doc
If this can be done I also like  to know if its possible to send this output to a specific place in this Document (Using a template)

Operating system: XP
Office 2002

Thx Dan  
0
Comment
Question by:Danny Kon
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12267881
As far as I know, there's not a way of editing an MS WORD document.. There's probably some utilies that can do something like this, but I'm sure it can't be done without external software.

I personally don't know any utilities to do this, hopefully someone else round here does..

Do you know how to create a Macro? If so, you can call a macro via DOS. :o\

gL,
[r.D]
0
 
LVL 8

Expert Comment

by:K_2K
ID: 12271940
You can format the whole thing in a txt file, then open it in Word, something like:

>file.txt echo.
>>file.txt echo    Heading: this is the report for %DATE%
>>file.txt echo.
>>file.txt echo.
>>file.txt echo.  Data follows
>>file.txt echo.

>>file.txt  CALL  batch_file_what_checks_some_values.bat  /Switches_for_Batch  "input path\filename if batch needs it.txt"

>>file.txt echo.
>>file.txt echo.
>>file.txt echo    Footer: this report ran by %USERNAME%
>>file.txt echo.

Start "Title: Please save the file"  "%ProgramFiles%\Microsoft Office\Office10\winword"  "file.txt"

:: end of batch or cleanup temp files or move word doc,  whatever.



If the document is already formatted, you could use use vbscript to access Word methods that are exposed to Windows, including search to find the place holder, and paste or insert to retrieve the clipboard or text file.  This way you could also do the saving straight from the vbscript with no user help.
 
0
 
LVL 6

Author Comment

by:Danny Kon
ID: 12272704
K_2K

Looks good but the last answer about the VBscript at a pre formated doc how to do this

Say i have a document saying something like:

To whom it may concern,

texttexttexttexttexttexttexttexttexttexttexttext

ANSWER ONE HERE ________

texttexttexttexttexttexttext

ANSWER TWO HERE _________

Greetings


and the the batch file has an output something like:

answer one = bla
answer two = blabla

Thanks Dan
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12278103
What exactly are you trying to do here?
Create some sort of test? Please elaborate; because if you're trying to do what I think you're trying to do, then there's much easier ways round this.

[r.D]
0
 
LVL 11

Expert Comment

by:Joseph O'Loughlin
ID: 12279687
Hi dannykon,
Have your batch file output to a text file.  
In a different file with the doc extention use <Ctrl>F9 and then INCLUDETEXT "Filename" - see online help.
If more complex formatting is needed I'd suggest txt2rtf convertion utilities.
usual disclaimers apply
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Author Comment

by:Danny Kon
ID: 12279711
DrWarezz,

I have a temporary assignment at a big company in the Netherlands
Some People at this assignment are testing software and the use SMS for this
To test the software the use msiexec and than they start/install to test if the installation is correct
The company require this testing has a standard Word template what has to be used and filled in
Yesterday i found an extra option on the msiexec where you can make a log and this log is telling exacly
what is happening.
This log is for the people who test the platform very hard to read so what i did is that i make a batch file with help from here offcourse what gives the needed info.
Now the problem is that management of this company want to have the output to the same document as what they used when testing this by hand also an simulair document can be used.

further info needed please tell me
Thanks in advance
Dan
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12287456
Okay..

you said:
"Say i have a document saying something like:

To whom it may concern,

texttexttexttexttexttexttexttexttexttexttexttext

ANSWER ONE HERE ________

texttexttexttexttexttexttext

ANSWER TWO HERE _________

Greetings


and the the batch file has an output something like:

answer one = bla
answer two = blabla"

This document that you refer to above, is it a microsoft word document? or just a .txt ?

If it's just a .txt, then I can help you code something to extract particular data from it, then output it to a word document (using K_2K's method :)).

[r.D]
0
 
LVL 6

Author Comment

by:Danny Kon
ID: 12289690
That's the problem this is a Word document with macro's and a specific layout so I think the best way to go is to
execute the batch from a macro in the word document but I have very little knowhow from VB.
I can manage to write a little VB code to execute the batch but than how to import the information in Word on the specific places
I know it can be done but need a VB expert

Help will be very much appriciated

Dan

0
 
LVL 8

Accepted Solution

by:
K_2K earned 500 total points
ID: 12354958
i b short on time,  but the jixt of it is in this little ditty i often update for a temporary work-around while i find better methods.  (Some of these become permanent solutions.)


First the batch to call it, assuming the batch file with the above output is bat1.bat:
this is a lot of work to find answers and set variables.  Obviously, you could drop the wscript line into bat1.bat and not need this at all, if you have the variables at your disposal inside that.  Then you'd only need the one .bat and no temp file in the middle.

:: parsefile.bat starts here
@echo off
setlocal
CALL bat1.bat > tempfile.out
for /f "tokens=1* delims==" %%K in ( tempfile.out ) do (
  if "%%K"=="answer one " set ans1=%%L
  if "%%K"=="answer two " set ans2=%%L
  )
wscript "WordEdit.vbs" "%ans1%" "%ans2%"
:: end of parsefile.bat


Now the .vbs file that does the work:  In short:
The top is two subs that don't do anything until called by the script.
' START is a comment only there to make it clear to us humans,  it does not help the script, nor can you goto it to instigate a loop.  You can lop off everything after the ' START and build your own script based on any window titles you could switch to and keys you could type there.  
oSH.Run is likely what you already know.  It starts word editting the document.
The rest sends keys to the word window as if you typed them from the keyboard.  
You can expect many virus programs to cry about this the first time it runs, but clicking the drop down box and selecting "authorize this script" should fix it permanently, or "always allow this script only"  or whatever your version requires.  Each time you edit the .vbs file, this has to be done once.  The input file and batch are strangely trusted by microsoft and symantec and mcafee and most others.


' WordEdit.vbs Starts here

option explicit
dim oSH, i, x, itmp, sQ, sDocName
Set oSH = WScript.CreateObject("WScript.Shell")
sQ = Chr(34)

sub Waitfor(x)
'  wait until title x can be activated or cannot
'      - good for waiting for dialog boxes to start or finish.
 itmp = false
 do
    itmp = oSH.AppActivate(x)
    Wscript.sleep 200
 loop until itmp
End Sub

sub dsk(x)
'  Delay 1/5th second, then send x as keyboard input.
'      - normal sendkeys is often too fast.
  Wscript.sleep 200
  oSH.SendKeys x
End Sub

' START

' fix document name to be editted here:  You MUST include the quotes even if no spaces are in the name
sDocName = "document saying something like.doc"

' start Word , pause a few seconds for doument to settle down, then switch to it again in case the user clicked outside it
oSH.Run "winword.exe " & sQ & sDocName & sQ
WaitFor sDocName & " - Microsoft Word"
Wscript.sleep 3000
WaitFor sDocName & " - Microsoft Word"

' send Alt+e and then e to open the Edit menu and start a Find and replace.
dsk "%e"
dsk "e"
WaitFor "Find and Replace"

' Enter the text for find:
dsk "ANSWER ONE HERE ________"
dsk "{Tab}"
' Enter the text for Replace:
dsk "ANSWER ONE HERE " & wscript.arguments(0)
' send Alt+A to replace all
dsk "%a"
' If string is there more than once, you could change this to use a few keys to search for the first and replace only that one
' wait for the replace finished dialog box and close it  
'    a pause is added here since there will be two windows with titles that match, and the one we want will
'    not open before this switches to the wrong one. value is 1000 ms per second
wscript.sleep 1000
WaitFor "Microsoft Word"
dsk "{Enter}"

WaitFor "Find and Replace"
' Enter the text for second find:
dsk "ANSWER TWO HERE ________"
dsk "{Tab}"
' Enter the text for Replace:
dsk "ANSWER TWO HERE " & wscript.arguments(1)
' send Alt+A to replace all
dsk "%A"
' If string is there more than once, you could change this to use a few keys to search for the first and replace only that one
' wait for the replace finished dialog box and close it  
'    a pause is added here since there will be two windows with titles that match, and the one we want will
'    not open before this switches to the wrong one. value is 1000 ms per second
wscript.sleep 1000
WaitFor "Microsoft Word"
dsk "{Enter}"

' close find dialog
WaitFor "Find and Replace"
dsk "{Esc}"

' Save
WaitFor sDocName & " - Microsoft Word"
' Close Word and save document
dsk "%f"
dsk "x"
wscript.sleep 1000
dsk "n"

' End of WordEdit.vbs

0
 
LVL 6

Author Comment

by:Danny Kon
ID: 12363906
K_2K

Funny and good solution I had only one problem that MSword had different language setting.
Its working ok but i am a little scared that if a lot of programs are open or if the computer just have a bad day :) that
the program will give problems but till now perfect job

THX

Dan
0
 
LVL 8

Expert Comment

by:K_2K
ID: 12366214
Thanks,  and your welcome, glad to help.


Yes,  there were a lot of problems with many programs open and an impatient guy (me) trying to do other work during these types of scripts the first time i wrote one like it.  Dont let that deter you from copying everything before the ' START to a new name and writing your own if you have a similar need.

The key is in the propper and often use of the waitfor(x) routine.  
A simple sleep would wait a while and then send a key to any program that might have been clicked or otherwize taken "focus" as the system sees it, causing wrong things to happen.
The waitfor(x), when applied after any sleeps or to wait for a new dialog box to appear, will constantly attempt to take focus of the window with a matching title starting with x.  If the window does not exist this will fail, sleep a bit, and try again.  The rest of the script is frozen there until focus on that window can happen.  Only then is control returned to the line after that waitfor(x) and keys can be sent knowing that window is ready to get them.

(\o/)  Happy Hunt and Pecking,
2K
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now