Renaming documents from text within the document using existing Ma

S

Suzanne C

I've been working with the following Macro to seperate a large document into
individual documents. The Macro saves these new documents as "test_1.doc",
"test_2.doc," etc. Everytime I run the macro, I have to then go back and
rename all the documents manually. The first line of the document always
begins with "Name:" and I rename the document simply by copying and pasting
the name that follows. Is there a way to have the macro do this for me?
Something that tells it to copy line 1 col 7-50 and paste into the FileName?

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

'A mail merge 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)

'Note: If a document does not end with a section break,
'substitute the following line of code for the one above:
'For I = 1 To ActiveDocument.Sections.Count

'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:\My Documents"
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
 
S

Steve Yandl

Suzanne,

I wasn't sure if you wanted to simply save the document with the name found
or also have the numbering you show in your sub. Try something like:

strName = RTrim(ActiveDocument.Range(6, 50).Text)
ActiveDocument.SaveAs FileName:="C:\Test\" & strName & ".doc"

Steve
 
S

Suzanne C

I had to alter it to:
strName = RTrim(ActiveDocument.Range(6, 50).Text)
ActiveDocument.SaveAs FileName:="strName" & ".doc",

but the only thing this did was save one document as strName.doc.
 
S

Steve Yandl

Suzanne,

Don't put the quote marks around strName. It's a variable that represents
the name extracted from line 1.

Steve
 
S

Suzanne C

That makes sense :) Unfortunately it still doesn't work. I get an error
stating that it is not a valid file name.
 
S

Steve Yandl

Are there any characters appearing on line one beside "Name:" and the name
of a person? Some characters would not be allowed as part of the file name.
Try changing the "6" to "7" so you have
strName = RTrim(ActiveDocument.Range(7, 50).Text)
and see if that works.

Steve
 
S

Suzanne C

Success! The problem was in the original document. After the person's name
there was a tab and then more information. I didn't realize that Word would
cound Tab and Enter as a character rather than a series of blank spaces. I
changed the original document so that after the person's name is on a line by
itself and use the space bar to give me extra space to account for people
with really long names.

This is going to save me so much time. Thanks for your help.
 
R

Russ

Suzanne,
One of the easy and correct ways to format white space in Word is to use
tabs instead of multiple, varying amounts of spaces (more work than you
need).

You can use the replace command to remove tab characters from a string and I
would also use a Trim() function for good measure, to trim any leading or
trailing spaces.:
StrName = Trim(Replace( ActiveDocument.Range(7, 50).Text, vbTab, ""))
 
R

Russ

Suzanne,
Another way to do it, if you **always** have the first tab character after
the name:
StrName = ActiveDocument.Paragraphs(1).Range.Text
StrName = Trim(Mid(StrName, 6, InStr(StrName, vbTab) - 6))

The first number 6 trims off the text "Name: "
The second number 6 just adjusts the character number where the tab
character was found because you did trim off the first six characters.
 
R

Russ

Suzanne,
Actually the numbers should be 7 and 7 to ignore the space character after
"Name:".
StrName = ActiveDocument.Paragraphs(1).Range.Text
StrName = Trim(Mid(StrName, 7, InStr(StrName, vbTab) - 7))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top