Batch File for Word Docs

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
iainprice
Posts: 5
Joined: 05 Sep 2013 23:56

Batch File for Word Docs

#1 Post by iainprice » 06 Sep 2013 00:00

Hi,

I have a list of 200 people, I have a word document.

I want to have a word document with each persons name in it.

One way is to put the author field as the name, copy the word document and call it a new name using a batch file... but how can I set the author name?

The other way is to do a find and replace for a certain string with a persons name....

Hmmmmm, any ideas?

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File for Word Docs

#2 Post by foxidrive » 06 Sep 2013 06:58

Is your list of people in a word document or a text file?

Batch files can't read the contents of word documents, just text files. They can't create word documents either.

iainprice
Posts: 5
Joined: 05 Sep 2013 23:56

Re: Batch File for Word Docs

#3 Post by iainprice » 06 Sep 2013 19:44

I can put the names anywhere. I was hoping for a simple set of batch commands to copy the file to a new name, then set the author field, then just repeat the set for the next name.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch File for Word Docs

#4 Post by ShadowThief » 06 Sep 2013 21:39

I'm very confused by what you're asking for. Here is how I interpret what you've said so far - please correct me:

You have a file that contains a list of 200 names (I assume one on each line, but I suppose they could also be comma-delimited or something).
You have a Word document that contains... some undescribed content. I'm imagining basically a form with an empty space for a name to go.

You want 200 Word documents, each containing the contents of the above Word document and one of the names from the file with the list of names.

iainprice
Posts: 5
Joined: 05 Sep 2013 23:56

Re: Batch File for Word Docs

#5 Post by iainprice » 06 Sep 2013 21:45

Yes, that's about it.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch File for Word Docs

#6 Post by ShadowThief » 06 Sep 2013 22:20

Like foxidrive said earlier, batch files can't read Word documents. If you absolutely, positively must have Word documents, I'd recommend trying something with Visual Basic macros because that's what's built into Word. However, if you can put both the list of names and the form in text documents, this becomes fairly simple.

To test, I made a tiny list of eight people and put them one on each line:

Code: Select all

John Doe
Steve Newman
The "Dwayne Johnson" Rock
Captain Hammer
Seamus Harper
That Asian Chick From 5SF
James Hook
Fred Rogers


And I made a really simple form:

Code: Select all

Form for Recommendation to Greatest People Ever Club
----------------------------------------------------
Name:
Age: Timeless
Benefit to Society: Doubtless
Human: Likely



And from there, the script outputs each line in the form file to a new file and if it reads the "Name" line, then it appends the name from the list.

Code: Select all

:Form_Filler
:: Takes a name from input.txt and sticks it in the blank space in form.txt
:: Repeat for each name in input.txt
@echo off
setlocal enabledelayedexpansion

set counter=0
for /f "tokens=*" %%A in (input.txt) do (
   for /f "tokens=*" %%B in (form.txt) do (
      if "%%B"=="Name: " (
         echo %%B%%A>>form_!counter!.txt
      ) else (
         echo %%B>>form_!counter!.txt
      )
   )
   set /a counter+=1
)



Right now, it outputs with filenames like form_0.txt and file_3.txt, etc. If you want something more descriptive, you could use the name of the person in the file instead of the counter (>>form_%%A.txt) or just have the name by itself (>>%%A.txt), but then you'd have to check for characters in the person's name like slashes and ampersands.

iainprice
Posts: 5
Joined: 05 Sep 2013 23:56

Re: Batch File for Word Docs

#7 Post by iainprice » 06 Sep 2013 22:26

Thanks. Yes must be word, will try macros. There is no way dos can change the author of a file?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch File for Word Docs

#8 Post by ShadowThief » 06 Sep 2013 22:46

You can take advantage of the fact that a .docx file is essentially a container like a .rar file and can be opened by a file archival program like WinRAR or 7Zip that has a command line interface and extract the files inside, grab the docProps\core.xml file, change the value inside the dc:creator tag, and recompile the document file, but that's a bit excessive for something that can almost certainly be more easily accomplished with Visual Basic macros.

iainprice
Posts: 5
Joined: 05 Sep 2013 23:56

Re: Batch File for Word Docs

#9 Post by iainprice » 07 Sep 2013 00:37

Can mail merge the names to make a large doc, I can use this macro to split on section breaks to make seperate docs but anyone good with macros? How can I use the <<name>> field from the merge in the file name on the macro?

I know this is a dos forum and you guys have been helpful. Thanks.



Sub BreakOnSection()
'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)

'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("\Section").Range.Copy

'Create a new document to paste text from clipboard.
Documents.Add
Selection.Paste

'Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

ChangeFileOpenDirectory "C:\"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
ActiveDocument.Close
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

Post Reply