Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to create about 80 Word documents that are identical except for a numerical identifier in the document. The identifier is the same as the document filename, eg. the document named "SN-100.doc" must have the string "SN-100" inside the document text.

Each unique document will reside in a folder location given by its unique numerical identifier.For example, the file directory hierarchy will look something like this:


What is the easiest and fastest method of doing this?

share|improve this question
Are you hoping for a zero programming answer? Because I will be very surprised if there is one. If you only need to do this one time then just manually do it because your will save yourself countless hours. – MonkeyZeus Oct 29 '13 at 20:04
Unless the documents themselves have some sort of a distinct characteristic (which isn't clear from the way you asked and more so from your answer) why not simply copy n times the file, then use a bulk rename tool to order and sort the documents? – Doktoro Reichard Oct 29 '13 at 22:17
@DoktoroReichard My primary difficultly is that each file does contain a small bit of information unique from the others. In my specific case I have the advantage that the unique information in the document is the same as the document filename without the filetype extension. – DakotaD Oct 30 '13 at 15:33

So this solution is probably too specific and too complicated for most users, but none the less it solved my problem.

The first part involved adding a Bookmark called "FileName" where I wanted my identifier. The following macro updates the Bookmark to the file name when the document is opened:

Private Sub Document_Open()
' Document_Open Macro
    Application.OnTime (Now + TimeValue("00:00:02")), "ProcessActiveDocument"

End Sub

Sub ProcessActiveDocument()
    Dim BmkNm As String, BmkRng As Range, FileName As String
    BmkNm = "FileName"
    FileName = ActiveDocument.Name
    If InStrRev(FileName, ".") > 0 Then FileName = Left(FileName, InStrRev(FileName, ".") - 1)
    If ActiveDocument.Bookmarks.Exists(BmkNm) Then
      Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
      If BmkRng.Text = FileName Then Exit Sub
      BmkRng.Text = FileName
      ActiveDocument.Bookmarks.Add BmkNm, BmkRng
      MsgBox "Bookmark: " & BmkNm & " not found."
    End If
    Set BmkRng = Nothing
End Sub

Of course macro security must be properly disengaged for this to work.

The next part was a Python script to duplicate the files and open each one triggering the macro.

import os
import shutil

ref_dir_base = "C:/SN"
word_doc = 'SN-'
word_ext = '.doc'
ref_num = 1
ref_str = str(1000+ref_num)[1:]
begin_num = 2
stop_num = 12
word_exec = '\"C:/Program Files (x86)/Microsoft Office/Office14/Winword.exe\"'

for i in range(begin_num, stop_num+1):
    num_str = str(1000+i)[1:]
    shutil.copytree(ref_dir_base+ref_str, ref_dir_base+num_str)
    old_doc_name = word_doc + str(ref_num) + word_ext
    new_doc_name = word_doc + str(i) + word_ext
    new_path = ref_dir_base + num_str + os.altsep
    os.rename(new_path + old_doc_name, new_path + new_doc_name)

for i in range(begin_num, stop_num+1):
    num_str = str(1000+i)[1:]   
    new_doc_name = word_doc + str(i) + word_ext
    new_path = ref_dir_base + num_str + os.altsep
    exec_param = '\"'+new_path+new_doc_name + '\"'
    exec_str = '\"'+word_exec+' '+exec_param +'\"'

The only problem with this approach is that the Word document is left open and unsaved so it requires the user to save the document for the changes to take effect.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.