K
Kathy
I am designing a template with several bookmarks that receive text from a
user form. What I need to do now is to take a name that is inserted and
pull out its initials. My code has to test to find out if there is a first
and last name, middle initial or if the name is a three-part name. The code
works great unless there is a middle initial. If there is a middle initial
in the name, it puts a space between the first and second initial. The
second and third initial are fine and the first and second are fine if there
is no middle initial. Here are what my results look like:
JOHN DOE --> JD
JOHN H. DOE --> J HD
I have tried setting up the bookmarks for the initials two different ways:
1. Three consecutive bookmarks --> III
2. Three consecutive spaces that I selected individually and assigned as a
bookmark (to have the text inserted into the bookmark versus right after the
bookmark).
The name bookmark is MD and is set right before the first character of the
name string.
The initial bookmarks are i1, i2, and i3.
I am proficient with Word but am new at developing code, so I'm sure my code
is heavy with extras. TIA! Kathy
Here is my code:
Sub mdinitials()
Selection.GoTo what:=wdGoToBookmark, Name:="MD"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.GoTo what:=wdGoToBookmark, Name:="i1"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Paste
Selection.GoTo what:=wdGoToBookmark, Name:="MD"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight unit:=wdWord, Count:=1
If Selection.Text = "." Then
Selection.MoveRight unit:=wdWord, Count:=1
Else: End If
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.GoTo what:=wdGoToBookmark, Name:="i2"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Paste
Selection.GoTo what:=wdGoToBookmark, Name:="MD"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight unit:=wdWord, Count:=2
If Selection.Text = "." Then
Selection.MoveRight unit:=wdWord, Count:=1
ElseIf Selection.Text = "," Then
Selection.GoTo what:=wdGoToBookmark, Name:="i3"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.TypeBackspace
GoTo 1
End If
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.GoTo what:=wdGoToBookmark, Name:="i3"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Paste
1
End Sub
user form. What I need to do now is to take a name that is inserted and
pull out its initials. My code has to test to find out if there is a first
and last name, middle initial or if the name is a three-part name. The code
works great unless there is a middle initial. If there is a middle initial
in the name, it puts a space between the first and second initial. The
second and third initial are fine and the first and second are fine if there
is no middle initial. Here are what my results look like:
JOHN DOE --> JD
JOHN H. DOE --> J HD
I have tried setting up the bookmarks for the initials two different ways:
1. Three consecutive bookmarks --> III
2. Three consecutive spaces that I selected individually and assigned as a
bookmark (to have the text inserted into the bookmark versus right after the
bookmark).
The name bookmark is MD and is set right before the first character of the
name string.
The initial bookmarks are i1, i2, and i3.
I am proficient with Word but am new at developing code, so I'm sure my code
is heavy with extras. TIA! Kathy
Here is my code:
Sub mdinitials()
Selection.GoTo what:=wdGoToBookmark, Name:="MD"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.GoTo what:=wdGoToBookmark, Name:="i1"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Paste
Selection.GoTo what:=wdGoToBookmark, Name:="MD"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight unit:=wdWord, Count:=1
If Selection.Text = "." Then
Selection.MoveRight unit:=wdWord, Count:=1
Else: End If
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.GoTo what:=wdGoToBookmark, Name:="i2"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Paste
Selection.GoTo what:=wdGoToBookmark, Name:="MD"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight unit:=wdWord, Count:=2
If Selection.Text = "." Then
Selection.MoveRight unit:=wdWord, Count:=1
ElseIf Selection.Text = "," Then
Selection.GoTo what:=wdGoToBookmark, Name:="i3"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.TypeBackspace
GoTo 1
End If
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.GoTo what:=wdGoToBookmark, Name:="i3"
With ActiveDocument.bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Paste
1
End Sub