HolidayBuyer's Guide

Office & Productivity Software forum

General discussion

Need help with macro in Word 2003

by LMillerMT / March 29, 2008 1:20 AM PDT

I'm currently using Word 2003 on a Microsoft XP system. I'm trying to create a macro within Word that will take some information out of the current document I'm working on and save the document with that string of text.

For example, I'm opening a document, typing in a person's name, then I'm trying to save the document to a certain folder with that string as part of the document name with a macro.

So if the person's name were John Doe, I would search for the text immediately preceding that, in this case "RE:," select everything behind the "RE:" string which would be "John Doe," and then save it to a certain folder.

The problem I'm running into is that the macro works properly one time only, then all subsequent files are also named John Doe.

So how do I create a macro that will refresh the person's name and not use John Doe each time?

Discussion is locked
You are posting a reply to: Need help with macro in Word 2003
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: Need help with macro in Word 2003
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Collapse -
Please show the source of macro you programmed.
by Kees Bakker / March 29, 2008 5:45 AM PDT

Somewhere inside it the clue is hidden.

Collapse -
Macro
by LMillerMT / March 29, 2008 11:08 PM PDT

'
' sv Macro
' Macro recorded 03/25/2008
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "RE:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdWord, Count:=4
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdStory
Application.Run MacroName:="Normal.NewMacros.PasteUnformatted"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Range.Case = wdNextCase
Selection.Range.Case = wdNextCase
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "RE:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdWord, Count:=2
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = "RE:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Application.Run MacroName:="Normal.NewMacros.PasteUnformatted"
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdStory
ChangeFileOpenDirectory "C:\Temp\JED\03March\Mar25_0328\"
ActiveDocument.SaveAs FileName:="JED_DoeJ_0754.rtf", FileFormat:= _
wdFormatRTF, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeBackspace
Selection.TypeParagraph
End Sub

Collapse -
Read the macro. This is what it says.
by Kees Bakker / March 30, 2008 4:37 AM PDT

ChangeFileOpenDirectory "C:\Temp\JED\03March\Mar25_0328\"
ActiveDocument.SaveAs FileName:="JED_DoeJ_0754.rtf",

which means:
Directory = "c:\temp\JED\03March\Mar25__0329"
Filename = "JED_DOEJ_0754.rtf"

Edit the code, so it fills two variables (say, mydir and myfil) with the right information and change the code above to:
ChangeFileOpenDirectory mydir
ActiveDocument.SaveAs FileName:=myfil,

That should do it.

Kees

Collapse -
Thank you!
by LMillerMT / March 30, 2008 10:32 PM PDT

I DID read the macro and I could see why it was happening, but I've never created a macro with variables, so I did not know how to do it.

That makes perfect sense, and I can't wait to broaden my macro writing now! I absolutely love using macros in my workday in order to make those common and time consuming tasks effortless.

Thanks again!

Popular Forums
icon
Computer Newbies 10,686 discussions
icon
Computer Help 54,365 discussions
icon
Laptops 21,181 discussions
icon
Networking & Wireless 16,313 discussions
icon
Phones 17,137 discussions
icon
Security 31,287 discussions
icon
TVs & Home Theaters 22,101 discussions
icon
Windows 7 8,164 discussions
icon
Windows 10 2,657 discussions

HOLIDAY GIFT GUIDE 2017

Cameras that make great holiday gifts

Let them start the new year with a step up in photo and video quality from a phone.