Formatting a string in document based on number of characters

G

gordobaggins

I have multiple doc files that I am formatting with a macro so I can
interface it to a medical application. My last challenge is that in
the document there is the following text:
Medical Record No.:
followed by a 4,5,or 6 digit number. I need to pad that number with
'MR' then enough 0's to make it 8 digits. I am fairly new to macros
and am trying to find a way to get the length of that number then act
accordingly.

Any help would be GREATLY appreciated.
 
G

gordobaggins

I was able to accomplish this by selecting the number and using the
selection.characters.count to determine the length then type in the
needed characters based on the count. Looks like this:

If Selection.Characters.Count = 6 Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="MR00"
End If
If Selection.Characters.Count = 5 Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="MR000"
End If
 
G

Greg Maxey

gordo,

This probably inefficient and not the best way but seems to work with
limited testing:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim tempString As String
Dim tempRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "(Medical Record No.:) ({1,}[0-9]{3,})"
While .Execute
Select Case True
Case Is = IsNumeric(Right(oRng.Text, 7))
tempString = (Right(oRng.Text, 6))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR00" & tempString
Case Is = IsNumeric(Right(oRng.Text, 6))
tempString = (Right(oRng.Text, 5))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR000" & tempString
Case Is = IsNumeric(Right(oRng.Text, 5))
tempString = (Right(oRng.Text, 4))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR0000" & tempString
Case Else
'Do Nothing
End Select
Wend
End With
End Sub
 
G

Greg Maxey

That smiley :) should be a colon and a right paren.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg said:
gordo,

This probably inefficient and not the best way but seems to work with
limited testing:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim tempString As String
Dim tempRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "(Medical Record No.:) ({1,}[0-9]{3,})"
While .Execute
Select Case True
Case Is = IsNumeric(Right(oRng.Text, 7))
tempString = (Right(oRng.Text, 6))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR00" & tempString
Case Is = IsNumeric(Right(oRng.Text, 6))
tempString = (Right(oRng.Text, 5))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR000" & tempString
Case Is = IsNumeric(Right(oRng.Text, 5))
tempString = (Right(oRng.Text, 4))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR0000" & tempString
Case Else
'Do Nothing
End Select
Wend
End With
End Sub


I have multiple doc files that I am formatting with a macro so I can
interface it to a medical application. My last challenge is that in
the document there is the following text:
Medical Record No.:
followed by a 4,5,or 6 digit number. I need to pad that number with
'MR' then enough 0's to make it 8 digits. I am fairly new to macros
and am trying to find a way to get the length of that number then act
accordingly.

Any help would be GREATLY appreciated.
 
D

Doug Robbins

This is a bit neater

Dim recno As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Medical Record No.: [0-9]{4,6}",
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set recno = Selection.Range
recno.Start = recno.Start + 20
recno.Text = "MR" & Format(recno.Text, "00000000")
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
Greg Maxey said:
That smiley :) should be a colon and a right paren.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg said:
gordo,

This probably inefficient and not the best way but seems to work with
limited testing:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim tempString As String
Dim tempRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "(Medical Record No.:) ({1,}[0-9]{3,})"
While .Execute
Select Case True
Case Is = IsNumeric(Right(oRng.Text, 7))
tempString = (Right(oRng.Text, 6))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR00" & tempString
Case Is = IsNumeric(Right(oRng.Text, 6))
tempString = (Right(oRng.Text, 5))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR000" & tempString
Case Is = IsNumeric(Right(oRng.Text, 5))
tempString = (Right(oRng.Text, 4))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR0000" & tempString
Case Else
'Do Nothing
End Select
Wend
End With
End Sub


I have multiple doc files that I am formatting with a macro so I can
interface it to a medical application. My last challenge is that in
the document there is the following text:
Medical Record No.:
followed by a 4,5,or 6 digit number. I need to pad that number with
'MR' then enough 0's to make it 8 digits. I am fairly new to macros
and am trying to find a way to get the length of that number then act
accordingly.

Any help would be GREATLY appreciated.
 
G

Greg Maxey

Doug,

I tested with two lines

Medical Record No.: 1234
Medical Record No.: 12345

For some reason using your code it replaces the first instance and then
exists the loop as done

I even cut the Select Case statement from my original macro (which doesn
loop twice) and replaced it with your:

' Set recno = oRng.Duplicate
' recno.Start = recno.Start + 20
' recno.Text = "MR" & Format(recno.Text, "00000000")

Still no joy.

I ran a message box after the second line and stetted the third (now fourth)
and the .execute loops twice. When I unstet the new fourth line it only
loops once and then exists.
Set recno = oRng.Duplicate
recno.Start = recno.Start + 20
Msgbox recon.text
' recno.Text = "MR" & Format(recno.Text, "00000000")

I'm really stumped.
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Doug said:
This is a bit neater

Dim recno As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Medical Record No.: [0-9]{4,6}",
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set recno = Selection.Range
recno.Start = recno.Start + 20
recno.Text = "MR" & Format(recno.Text, "00000000")
Loop
End With



Doug Robbins - Word MVP
Greg Maxey said:
That smiley :) should be a colon and a right paren.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg said:
gordo,

This probably inefficient and not the best way but seems to work
with limited testing:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim tempString As String
Dim tempRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "(Medical Record No.:) ({1,}[0-9]{3,})"
While .Execute
Select Case True
Case Is = IsNumeric(Right(oRng.Text, 7))
tempString = (Right(oRng.Text, 6))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR00" & tempString
Case Is = IsNumeric(Right(oRng.Text, 6))
tempString = (Right(oRng.Text, 5))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR000" & tempString
Case Is = IsNumeric(Right(oRng.Text, 5))
tempString = (Right(oRng.Text, 4))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR0000" & tempString
Case Else
'Do Nothing
End Select
Wend
End With
End Sub


(e-mail address removed) wrote:
I have multiple doc files that I am formatting with a macro so I
can interface it to a medical application. My last challenge is
that in the document there is the following text:
Medical Record No.:
followed by a 4,5,or 6 digit number. I need to pad that number
with 'MR' then enough 0's to make it 8 digits. I am fairly new to
macros and am trying to find a way to get the length of that
number then act accordingly.

Any help would be GREATLY appreciated.
 
G

Greg Maxey

Doug

I cobbled the following together which seems to work similiar to your line,
but I am still stumped as to why I couldn't get your solution to work:

Sub ScratchMacro3()
Dim oRng As Word.Range
Dim tempString As String
Dim tempRng As Word.Range
Dim recno As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "Medical Record No.: {1,}[0-9]{3,6}[!0-9]"
While .Execute
oRng.End = oRng.End - 1
tempString = Right(oRng.Text, Len(oRng.Text) - 20)
Set recno = oRng.Duplicate
recno.Text = "Medical Record No.: " & Format(tempString, "00000000")
Wend
End With
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg said:
Doug,

I tested with two lines

Medical Record No.: 1234
Medical Record No.: 12345

For some reason using your code it replaces the first instance and
then exists the loop as done

I even cut the Select Case statement from my original macro (which
doesn loop twice) and replaced it with your:

' Set recno = oRng.Duplicate
' recno.Start = recno.Start + 20
' recno.Text = "MR" & Format(recno.Text, "00000000")

Still no joy.

I ran a message box after the second line and stetted the third (now
fourth) and the .execute loops twice. When I unstet the new fourth
line it only loops once and then exists.
Set recno = oRng.Duplicate
recno.Start = recno.Start + 20
Msgbox recon.text
' recno.Text = "MR" & Format(recno.Text, "00000000")

I'm really stumped.

Doug said:
This is a bit neater

Dim recno As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Medical Record No.: [0-9]{4,6}",
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set recno = Selection.Range
recno.Start = recno.Start + 20
recno.Text = "MR" & Format(recno.Text, "00000000")
Loop
End With



Doug Robbins - Word MVP
Greg Maxey said:
That smiley :) should be a colon and a right paren.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg Maxey wrote:
gordo,

This probably inefficient and not the best way but seems to work
with limited testing:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim tempString As String
Dim tempRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "(Medical Record No.:) ({1,}[0-9]{3,})"
While .Execute
Select Case True
Case Is = IsNumeric(Right(oRng.Text, 7))
tempString = (Right(oRng.Text, 6))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR00" & tempString
Case Is = IsNumeric(Right(oRng.Text, 6))
tempString = (Right(oRng.Text, 5))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR000" & tempString
Case Is = IsNumeric(Right(oRng.Text, 5))
tempString = (Right(oRng.Text, 4))
Set tempRng = oRng.Duplicate
tempRng.Text = "Medical Record.: MR0000" & tempString
Case Else
'Do Nothing
End Select
Wend
End With
End Sub


(e-mail address removed) wrote:
I have multiple doc files that I am formatting with a macro so I
can interface it to a medical application. My last challenge is
that in the document there is the following text:
Medical Record No.:
followed by a 4,5,or 6 digit number. I need to pad that number
with 'MR' then enough 0's to make it 8 digits. I am fairly new to
macros and am trying to find a way to get the length of that
number then act accordingly.

Any help would be GREATLY appreciated.
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Doug

I cobbled the following together which seems to work similiar to your
line, but I am still stumped as to why I couldn't get your solution
to work:


Hi Greg,

Try this version. I just added a Selection.Collapse, otherwise the "Execute"
runs in the currently selected text (the first Find ext) and of course fails
to find any more occurrences in the document...

Dim recno As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Medical Record No.: [0-9]{4;6}", _
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set recno = Selection.Range
recno.Start = recno.Start + 20
recno.Text = "MR" & Format(recno.Text, "00000000")
Selection.Collapse wdCollapseEnd
Loop
End With

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg

JGM,

Your solution works. Seems I often hear that using range over
selection is preferred. I thought that I had tried the code below last
night. Since it works now and didn't then, I must not have tried it.

Sub ScratchMacro1()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "Medical Record No.: {1,}[0-9]{4,6}"
While .Execute
oRng.Start = oRng.Start + 20
oRng.Text = "MR" & Format(oRng.Text, "00000000")
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Can you explain why the Collapse step is needed in your example and my
new code above and it wasn't needed in the first method that I
proposed? Based on your explaination above, I seems like it should
have been required. Thanks Senei.
 
J

Jean-Guy Marcil

Greg was telling us:
Greg nous racontait que :
JGM,

Your solution works. Seems I often hear that using range over
selection is preferred. I thought that I had tried the code below
last night. Since it works now and didn't then, I must not have tried
it.

Sub ScratchMacro1()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "Medical Record No.: {1,}[0-9]{4,6}"
While .Execute
oRng.Start = oRng.Start + 20
oRng.Text = "MR" & Format(oRng.Text, "00000000")
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Can you explain why the Collapse step is needed in your example and my
new code above and it wasn't needed in the first method that I
proposed? Based on your explaination above, I seems like it should
have been required. Thanks Senei.

I think it is because you used a Range object, and then when you did find a
match, you duplicated the found range into a new range object, so the
original is left untouched, so Word carries on with the search as it would
normally.

But I am no expert with the Find/Replace stuff, I am just starting to
understand its intricacies. It can get hairy sometimes...!


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg

JGM,

That was my thoughts to, but then this doesn't work with the collapse
statement stetted out:

Sub Test()
Dim oRng As Word.Range
Dim tempRng As Word.Range

Set oRng = ActiveDocument.Range

With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.MatchWholeWord = True
.Wrap = wdFindStop
.Text = "Medical Record No.: {1,}[0-9]{4,6}"
While .Execute
Set tempRng = oRng.Duplicate
tempRng.Start = tempRng.Start + 20
tempRng.Text = "MR" & Format(tempRng.Text, "00000000")
'tempoRng.Collapse wdCollapseEnd
Wend
End With
End Sub

We may need a Master Sensei :)
 
D

Doug Robbins

The .Wrap should have been set to wdFindContinue

The following handles multiple instances. My testing wasn't thorough
enough.

Dim recno As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Medical Record No.: [0-9]{4,6}",
MatchWildcards:=True, Wrap:=wdFindContinue, Forward:=True) = True
Set recno = Selection.Range
recno.Start = recno.Start + 20
recno.Text = "MR" & Format(recno.Text, "00000000")
Loop
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Greg

Doug,

Well that certainly works. I still can't figure out why first proposed
solution worked (and still does) with .Wrap set to wdFindStop while
versions without the Select Case piece need either the .Collapse or
wdFindContinue coding. Very perplexing as Word often is ;-)
 
G

gordobaggins

Thanks so much to everyone! The above code works GREAT and was much
more effiecient than my Selection.Characters.Count code.

I really appreciate everyones help.

Gordy
 

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