I am new to macros in Word, but do have some experience in Excel and Access. I just need a little help getting started.
I have a Word document that is created by a main frame tool using data from the mainframe. The challenge is the Name field is always both first and last name, (no way for the mainframe to send them as separate fields) AND the name is ALWAYS in upper case.
The goal is to create a macro that will modify the attached in the following way:
1. Find line five (Name) and convert it to mixed case (wdTitleWord I think)
2. Find the "Dear" line, delete the space after the first name and delete the last name
Note: Some names will come in with a middle initial or middle name so need to allow for deleting middle name in the "Dear" line if it exists.
Note2: Will be transitioning from Office 2003 to Office 2007 right away, so a 2007 or greater solution would be fine.
Any help is GREATLY appreciated.
Ken
Macros in Word
-
- 3StarLounger
- Posts: 308
- Joined: 24 Feb 2010, 13:41
Macros in Word
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 78535
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macros in Word
Try this:
Code: Select all
Sub FixNames()
ActiveDocument.Paragraphs(5).Range.Case = wdTitleWord
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.Text = "^pDear "
.Execute
End With
Selection.MoveRight Unit:=wdWord
Selection.Range.Words(1).Case = wdTitleWord
Selection.MoveRight Unit:=wdWord
Selection.MoveStart Unit:=wdCharacter, Count:=-1
Selection.MoveEndUntil CSet:=":"
Selection.Delete
End Sub
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 12614
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Macros in Word
There is probably an easier way to do this, but the following works on your sample document. It assumes that the text you want to change will always be at lines 5 and 12, if this might change then the code will need to be different.
Code: Select all
Public Sub ProcessLetter()
Dim doc As Document
Dim rng As Range
Set doc = ActiveDocument
With doc
.Paragraphs(5).Range.Case = wdTitleWord
Set rng = .Paragraphs(12).Range
With rng
.MoveStart Unit:=wdWord, Count:=2
.MoveStart Unit:=wdCharacter, Count:=-1
.MoveEnd Unit:=wdCharacter, Count:=-2
.Delete
End With
End With
End Sub
StuartR
-
- Administrator
- Posts: 78535
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macros in Word
The address part might have a variable number of lines...StuartR wrote:It assumes that the text you want to change will always be at lines 5 and 12, if this might change then the code will need to be different.
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 308
- Joined: 24 Feb 2010, 13:41
Re: Macros in Word
Stuart,
Thanks for sharing! Line 5 will be constant, but the "Dear" line can vary. I used Hans' code and it accomplished to goal.
Ken
Thanks for sharing! Line 5 will be constant, but the "Dear" line can vary. I used Hans' code and it accomplished to goal.
Ken