Acroymns

S

Sahana

I got this macro in this forum some time back and I find some problem
with it. My previous post is pretty old so it s not sensible to reopen
it. So, I thought I will be good to open a new post.

My need: I want macros NOT to add word "The" before abbreviation IF
placed between ()

Code 1:

This perfectly check for abbreviations at every instance. This again I
found in this forum.

Sub TheBeforeAcronym()
Dim myRange As Range
Dim oRng As Range
Dim rslt As VbMsgBoxResult
Set myRange = ActiveDocument.Range
With myRange.Find
..Text = "<([A-Z]{2,})>"
..MatchWildcards = True
..Wrap = wdFindStop
..Forward = True
While .Execute
myRange.Select
Set oRng = myRange.Duplicate
oRng.Move wdCharacter, -5
oRng.MoveEnd wdCharacter, 4
If Not oRng.Text = "the " Then
rslt = MsgBox(Prompt:="Add 'the' before this acronym?",
Buttons:=vbYesNoCancel)
If rslt = vbCancel Then Exit Sub
If rslt = vbYes Then
Selection.InsertBefore "the "
myRange.Collapse wdCollapseEnd
End If
End If
myRange.Collapse wdCollapseEnd
Wend
End With
Set myRange = Nothing
End Sub


Code 2:

Some modifications are done but is not suiting my needs.

Sub TheBeforeAcronym()
Dim myRange As Range
Dim rslt As VbMsgBoxResult


Set myRange = ActiveDocument.Range
With myRange
.Find.Text = "<([A-Z]{2,})>"
.Find.MatchWildcards = True
.Find.Wrap = wdFindStop
.Find.Forward = True
While .Find.Execute
.MoveStart wdCharacter, -4
.MoveEnd wdCharacter, 1
If Not (.Characters(4) = "(" And _
.Characters.Last = ")") And _
Not Left(.Text, 4) = "the " Then
.MoveStart wdCharacter, 4
.Select
rslt = MsgBox(Prompt:= _
"Add 'the' before this acronym?", _
Buttons:=vbYesNoCancel)
If rslt = vbCancel Then Exit Sub
If rslt = vbYes Then
Selection.InsertBefore "the "
Application.ScreenRefresh
End If
End If
.MoveEnd wdCharacter, -1
myRange.Collapse wdCollapseEnd
Wend
Selection.Collapse Direction:=wdCollapseEnd
End With
Set myRange = Nothing
End Sub


I hope I get some help here.

Thanks

Sahana
 
G

Graham Mayor

Your modification appears to achieve your stated aim (apart from the
possible need to capitalise 'The' if it starts a sentence) so in what way
does it not suit your needs?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sahana

Your modification appears to achieve your stated aim (apart from the
possible need to capitalise 'The' if it starts a sentence) so in what way
does it not suit your needs?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


I got this macro in this forum some time back and I find some problem
with it. My previous post is pretty old so it s not sensible to reopen
it. So, I thought I will be good to open a new post.
My need: I want macros NOT to add word "The" before abbreviation IF
placed between ()
This perfectly check for abbreviations at every instance. This again I
found in this forum.
Sub TheBeforeAcronym()
Dim myRange As Range
Dim oRng As Range
Dim rslt As VbMsgBoxResult
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = "<([A-Z]{2,})>"
.MatchWildcards = True
.Wrap = wdFindStop
.Forward = True
While .Execute
myRange.Select
Set oRng = myRange.Duplicate
oRng.Move wdCharacter, -5
oRng.MoveEnd wdCharacter, 4
If Not oRng.Text = "the " Then
rslt = MsgBox(Prompt:="Add 'the' before this acronym?",
Buttons:=vbYesNoCancel)
If rslt = vbCancel Then Exit Sub
If rslt = vbYes Then
Selection.InsertBefore "the "
myRange.Collapse wdCollapseEnd
End If
End If
myRange.Collapse wdCollapseEnd
Wend
End With
Set myRange = Nothing
End Sub
Some modifications are done but is not suiting my needs.
Sub TheBeforeAcronym()
   Dim myRange As Range
   Dim rslt As VbMsgBoxResult
   Set myRange = ActiveDocument.Range
   With myRange
       .Find.Text = "<([A-Z]{2,})>"
       .Find.MatchWildcards = True
       .Find.Wrap = wdFindStop
       .Find.Forward = True
       While .Find.Execute
           .MoveStart wdCharacter, -4
           .MoveEnd wdCharacter, 1
           If Not (.Characters(4) = "(" And _
               .Characters.Last = ")") And _
               Not Left(.Text, 4) = "the " Then
               .MoveStart wdCharacter, 4
               .Select
               rslt = MsgBox(Prompt:= _
                   "Add 'the' before this acronym?", _
                   Buttons:=vbYesNoCancel)
               If rslt = vbCancel Then Exit Sub
               If rslt = vbYes Then
                   Selection.InsertBefore "the "
                   Application.ScreenRefresh
               End If
           End If
           .MoveEnd wdCharacter, -1
           myRange.Collapse wdCollapseEnd
       Wend
       Selection.Collapse Direction:=wdCollapseEnd
   End With
   Set myRange = Nothing
End Sub
I hope I get some help here.

Sahana- Hide quoted text -

- Show quoted text -

I want macros to ignore to add THE if abbreviation is placed between
().

Ex:

ABC
Macros wil add THE before ABC

(ABC)
I want macros to ignore to add THE as it is placed between()
 
G

Graham Mayor

Your macro does exactly that?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sahana

Your macro does exactly that?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>





- Show quoted text -

Please test the following data. I faced problems in these portions.
Hope you can help me solve the problem.

Case 1

Test
(JHJFHDJ)
UNICEFLF
UNICE
INIFHD (KHFJKDFRD)
Technically, macros should not test abbreviations between () but
macros highlight JFHDJ the last five words of the JHJFDL placed in the
brackets.


Case 2

Test
UNICEFLF
UNICE
INIFHD (KHFJKDFRD)
The macros highlights EFLF and pops up message box as given the macro

Can you help me with this?
 
G

Graham Mayor

I managed to delete your last message before replying :( However I have
tested with your example text and cannot reproduce the issue. The macro
copied from your message correctly identifies all the strings except those
in brackets.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sahana

I managed to delete your last message before replying :( However I have
tested with your example text and cannot reproduce the issue. The macro
copied from your message correctly identifies all the strings except those
in brackets.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>






- Show quoted text -

Can you explain why I m facing the issue while the same code worked
cool for you?
Has my settings changed or something
 
S

Sahana

Can you explain why I m facing the issue while the same code worked
cool for you?
Has my settings changed or something- Hide quoted text -

- Show quoted text -

You can type ABC and I get add the message below ABC, which is wrong.

Can you suggest me a way out of this so that I can get a solution
 
G

Graham Mayor

You can type ABC and I get add the message below ABC, which is wrong.
Can you suggest me a way out of this so that I can get a solution

Don't test with documents that contain only three characters when the macro
is programmed to move the ends of the range outside the available range.

The macro will fail if the acronym is the *only* word in the document, or if
it is the *first* or *last* Word in the document. Those conditions would
have trapped if you want to account for them.

If Len(.Text) = Len(ActiveDocument.Range.Text) - 1 Then 'acronym is the only
word in the document
If .Start = ActiveDocument.Range.Start Then 'Acronym is the first Word in
the document
If .End = ActiveDocument.Range.End - 1 Then 'Acronym is the last word in
the document
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

Hi,

I think that it would be more accurate to say that the macro (Code 2) cannot
handle the case in which an acronym beginning within the first 4 characters
in the doc is found. There is no problem with an acronym at the end of the
doc because the complete Range of a doc always extends beyond the last word.
Therefore, the condition that you need to handle is "If .Start < 4". The
following modified version of the macro handles this situation by popping up
a message, but takes no other action. Of course, you can write more elaborate
code to handle this case.

Sub TheBeforeAcronym()
Dim myRange As Range
Dim rslt As VbMsgBoxResult

Set myRange = ActiveDocument.Range
With myRange
.Find.Text = "<([A-Z]{2,})>"
.Find.MatchWildcards = True
.Find.Wrap = wdFindStop
.Find.Forward = True
While .Find.Execute
If .start < 4 Then
MsgBox "The acronym " & .Text & _
" was found at the beginning of the doc."
Else
.MoveStart wdCharacter, -4
.MoveEnd wdCharacter, 1
If Not (.Characters(4) = "(" And _
.Characters.Last = ")") And _
Not Left(.Text, 4) = "the " Then
.MoveStart wdCharacter, 4
.Select
rslt = MsgBox(Prompt:= _
"Add 'the' before this acronym?", _
Buttons:=vbYesNoCancel)
If rslt = vbCancel Then Exit Sub
If rslt = vbYes Then
Selection.InsertBefore "the "
Application.ScreenRefresh
End If
End If
.MoveEnd wdCharacter, -1
End If
myRange.Collapse wdCollapseEnd
Wend
Selection.Collapse Direction:=wdCollapseEnd
End With
Set myRange = Nothing
End Sub
 
S

Sahana

Hi,

I think that it would be more accurate to say that the macro (Code 2) cannot
handle the case in which an acronym beginning within the first 4 characters
in the doc is found. There is no problem with an acronym at the end of the
doc because the complete Range of a doc always extends beyond the last word.
Therefore, the condition that you need to handle is "If .Start < 4". The
following modified version of the macro handles this situation by poppingup
a message, but takes no other action. Of course, you can write more elaborate
code to handle this case.

Sub TheBeforeAcronym()
    Dim myRange As Range
    Dim rslt As VbMsgBoxResult

    Set myRange = ActiveDocument.Range
    With myRange
        .Find.Text = "<([A-Z]{2,})>"
        .Find.MatchWildcards = True
        .Find.Wrap = wdFindStop
        .Find.Forward = True
        While .Find.Execute
            If .start < 4 Then
                MsgBox "The acronym " & .Text & _
                " was found at the beginning of the doc."
            Else
                .MoveStart wdCharacter, -4
                .MoveEnd wdCharacter, 1
                If Not (.Characters(4) = "(" And _
                    .Characters.Last = ")") And _
                    Not Left(.Text, 4) = "the " Then
                    .MoveStart wdCharacter, 4
                    .Select
                    rslt = MsgBox(Prompt:= _
                        "Add 'the' before this acronym?", _
                        Buttons:=vbYesNoCancel)
                    If rslt = vbCancel Then Exit Sub
                    If rslt = vbYes Then
                        Selection.InsertBefore "the "
                        Application.ScreenRefresh
                    End If
            End If
            .MoveEnd wdCharacter, -1
            End If
            myRange.Collapse wdCollapseEnd
        Wend
        Selection.Collapse Direction:=wdCollapseEnd
    End With
    Set myRange = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz



Don't test with documents that contain only three characters when the macro
is programmed to move the ends of the range outside the available range..
The macro will fail if the acronym is the *only* word in the document, or if
it is the *first* or *last* Word in the document. Those conditions would
have trapped if you want to account for them.
If Len(.Text) = Len(ActiveDocument.Range.Text) - 1 Then 'acronym is the only
word in the document
If .Start = ActiveDocument.Range.Start Then 'Acronym is the first Word in
the document
If .End = ActiveDocument.Range.End - 1 Then  'Acronym is the last word in
the document
My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>- Hide quoted text -

- Show quoted text -

I notice that macros continues to skip first abbreviation. Can this be
prevented in some way or other? Where macros can search for first
instance as well?
 
P

Pesach Shelnitz

Hi Sahana,

You should be seeing a pop-up message for an acronym at the beginning of a
document. If this isn't enough action, you can replace the lines

If .start < 4 Then
MsgBox "The acronym " & .Text & _
" was found at the beginning of the doc."

by the lines

If .start < 4 Then
.Select
rslt = MsgBox(Prompt:= _
"Add 'the' before this acronym?", _
Buttons:=vbYesNoCancel)
If rslt = vbCancel Then Exit Sub
If rslt = vbYes Then
Selection.InsertBefore "the "
Application.ScreenRefresh
End If

Note that the macro will still not check for parentheses around an acronym
at the very beginning of a doc.

--
Hope this helps,
Pesach Shelnitz


Sahana said:
Hi,

I think that it would be more accurate to say that the macro (Code 2) cannot
handle the case in which an acronym beginning within the first 4 characters
in the doc is found. There is no problem with an acronym at the end of the
doc because the complete Range of a doc always extends beyond the last word.
Therefore, the condition that you need to handle is "If .Start < 4". The
following modified version of the macro handles this situation by popping up
a message, but takes no other action. Of course, you can write more elaborate
code to handle this case.

Sub TheBeforeAcronym()
Dim myRange As Range
Dim rslt As VbMsgBoxResult

Set myRange = ActiveDocument.Range
With myRange
.Find.Text = "<([A-Z]{2,})>"
.Find.MatchWildcards = True
.Find.Wrap = wdFindStop
.Find.Forward = True
While .Find.Execute
If .start < 4 Then
MsgBox "The acronym " & .Text & _
" was found at the beginning of the doc."
Else
.MoveStart wdCharacter, -4
.MoveEnd wdCharacter, 1
If Not (.Characters(4) = "(" And _
.Characters.Last = ")") And _
Not Left(.Text, 4) = "the " Then
.MoveStart wdCharacter, 4
.Select
rslt = MsgBox(Prompt:= _
"Add 'the' before this acronym?", _
Buttons:=vbYesNoCancel)
If rslt = vbCancel Then Exit Sub
If rslt = vbYes Then
Selection.InsertBefore "the "
Application.ScreenRefresh
End If
End If
.MoveEnd wdCharacter, -1
End If
myRange.Collapse wdCollapseEnd
Wend
Selection.Collapse Direction:=wdCollapseEnd
End With
Set myRange = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz



Graham Mayor said:
You can type ABC and I get add the message below ABC, which is wrong.
Can you suggest me a way out of this so that I can get a solution
Don't test with documents that contain only three characters when the macro
is programmed to move the ends of the range outside the available range..
The macro will fail if the acronym is the *only* word in the document, or if
it is the *first* or *last* Word in the document. Those conditions would
have trapped if you want to account for them.
If Len(.Text) = Len(ActiveDocument.Range.Text) - 1 Then 'acronym is the only
word in the document
If .Start = ActiveDocument.Range.Start Then 'Acronym is the first Word in
the document
If .End = ActiveDocument.Range.End - 1 Then 'Acronym is the last word in
the document
My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>- Hide quoted text -

- Show quoted text -

I notice that macros continues to skip first abbreviation. Can this be
prevented in some way or other? Where macros can search for first
instance as well?
 

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