Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Renaming documents from text within the document using existing Ma

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Suzanne C - 17 Oct 2007 18:06 GMT
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
Steve Yandl - 17 Oct 2007 19:02 GMT
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

> I've been working with the following Macro to seperate a large document
> into
[quoted text clipped - 38 lines]
>   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
> End Sub
Suzanne C - 17 Oct 2007 20:00 GMT
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.

> Suzanne,
>
[quoted text clipped - 48 lines]
> >   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
> > End Sub
Steve Yandl - 17 Oct 2007 20:48 GMT
Suzanne,

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

Steve

>I had to alter it to:
> strName = RTrim(ActiveDocument.Range(6, 50).Text)
[quoted text clipped - 58 lines]
>> >   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
>> > End Sub
Suzanne C - 17 Oct 2007 21:41 GMT
That makes sense :) Unfortunately it still doesn't work. I get an error
stating that it is not a valid file name.

> Suzanne,
>
[quoted text clipped - 65 lines]
> >> >   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
> >> > End Sub
Steve Yandl - 17 Oct 2007 22:35 GMT
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

> That makes sense :) Unfortunately it still doesn't work. I get an error
> stating that it is not a valid file name.
[quoted text clipped - 72 lines]
>> >> >   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
>> >> > End Sub
Suzanne C - 18 Oct 2007 16:50 GMT
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.

> 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.
[quoted text clipped - 80 lines]
> >> >> >   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
> >> >> > End Sub
Russ - 19 Oct 2007 06:30 GMT
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, ""))

> 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
[quoted text clipped - 89 lines]
>>>>>>>   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
>>>>>>> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 19 Oct 2007 07:10 GMT
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.

> Suzanne,
> One of the easy and correct ways to format white space in Word is to use
[quoted text clipped - 99 lines]
>>>>>>>>   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
>>>>>>>> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 19 Oct 2007 07:23 GMT
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))

> Suzanne,
> Another way to do it, if you **always** have the first tab character after
[quoted text clipped - 111 lines]
>>>>>>>>>   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
>>>>>>>>> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.