Macros Looping Help Needed

Microsoft Office word 2003 step by step
January 6, 2010 at 06:52:46
Specs: Windows XP
I have created a macros (first time) in a Word 2003 document in hopes that it will help automate creation of several hundred docs.

I created a "pretty version" of the document (tables, formatting, pictures) and created a macros to pull information from a plain Word document that just contains text.

After countless hours of tweaking, I got it to work on the first doc... However, now I have to do that to about 200 other docs as well...

In the macros I created, it references two specific docs through the code (they are both saved in same folder:
1. Pretty_template.doc
2. plaindoc1.doc

I have it set up where after macros runs, it saves the new doc as Pretty_1.doc.

How can I automatically replicate this entire macros to be applied to the other 199 plain docs that are saved in the same all I have to do is run it once and all change and save?

I have the macros saved in General.

Any help is greatly appreciate as I have a 2day deadline which is why I learned this in the first place... aweseome tool though!

See More: Macros Looping Help Needed

Report •

January 6, 2010 at 08:14:50

Incorporate this into your macro. It creates new numbered filenames. You can wrap this around the outside of your existing macro. Move any existing Dim statements up to the top, so that they are not repeated with each loop.

If you are not using Option Explicit before any of your code i.e., before your first subroutine, I suggest you add it. It helps by flagging up errors due to typos, such as typing strFilesStem instead of strFileStem. Dim all your variables.
Also prefixing variables with their type, e.g., intCount as it is an integer number, or strFilename as it is a text string, reduces the chance of errors by reminding you what each variable is.

Option Explicit
Sub Newfiles()

Dim intCount As Integer
Dim strFilename As String
Dim strFileStem As String
Dim strFileCount As String

'set stem for all filenames
strFileStem = "Pretty_"

For intCount = 1 To 199
    'create a new filename
    'start by converting the counter 'intCount' into text, intCount=1 becomes 001
    strFileCount = Format(intCount, "000")
    'put the stem, the counter and the file type together
    strFilename = strFileStem & strFileCount & ".doc"
    'use your existing code to create the new document
    'then use the new filename to save your new file here
    'the filename looks like this Pretty_001.doc
Next intCount
End Sub

Hope this helps.


Report •
Related Solutions

Ask Question