Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-03-2015, 05:42 AM
AlexandarR AlexandarR is offline How to insert comments thro' macro Windows XP How to insert comments thro' macro Office 2003
Novice
How to insert comments thro' macro
 
Join Date: Mar 2015
Location: Chennai, Tamilnadu, India
Posts: 7
AlexandarR is on a distinguished road
Thumbs up How to insert comments thro' macro

Hi!
I want to find "UK" or "U.K." within selection <AFF>...</AFF>, and replace it to "United Kingdom". More than that, I want to insert a comment "Country name changed. Please check" only if replacement done.



<AFF>Department of Physics and Astronomy and National Center for Physical Acoustics, University of Mississippi, P.O. Box 1848, 1 Chucky Mullins Drive, University, Mississippi 38677</AFF>

I have recorded a macro, except inserting comment.

Please help. Thanks in advance.

CODE:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 3/3/2015 by alexandarr
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "\<AFF\>*\<\/AFF\>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Application.Run MacroName:="MathTypeCommands.UIWrappers2003.EditPa ste"
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "UK"
.Replacement.Text = "United Kingdom"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "U.K."
.Replacement.Text = "United Kingdom"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.Copy
Windows("AI-JAS#150100_ed.doc").Activate
Application.Run MacroName:="MathTypeCommands.UIWrappers2003.EditPa ste"
End Sub


Reply With Quote
  #2  
Old 03-03-2015, 06:48 AM
gmayor's Avatar
gmayor gmayor is offline How to insert comments thro' macro Windows 7 64bit How to insert comments thro' macro Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It is not clear what your original macro is about with respect to calls to an unidentified macro, an added document and a named document, but the following will look in each text string marked <AFF>...</AFF>, and replace UK or U.K with "United Kingdom" and add a comment to that effect. UK or U.K not tagged with <AFF>...</AFF> are ignored.

Code:
Option Explicit
Sub ChangeUK()
Dim vFindText As Variant
Dim sReplaceText As String
Dim oRng As Range
Dim oSearch As Range
Dim oFound As Range
Dim i As Long
Dim sAsk As String
    vFindText = Array("UK", "U.K.")
    sReplaceText = "United Kingdom"
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:="\<AFF\>*\<\/AFF\>", MatchWildcards:=True)
            Set oSearch = oRng
            For i = 0 To UBound(vFindText)
                Set oFound = oSearch
                With oFound.Find
                    .ClearFormatting
                    .Replacement.ClearFormatting
                    Do While .Execute(FindText:=vFindText(i), _
                                      MatchWholeWord:=True, _
                                      MatchWildcards:=True, _
                                      Forward:=True, _
                                      Wrap:=wdFindStop) = True
                        oFound.Text = sReplaceText
                        oFound.Comments.Add oFound, "Country name changed. Please check"
                        oFound.Collapse wdCollapseEnd
                        If oFound.End >= oSearch.End Then Exit Do
                    Loop
                End With
            Next i
        Loop
    End With
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-03-2015, 07:43 AM
AlexandarR AlexandarR is offline How to insert comments thro' macro Windows XP How to insert comments thro' macro Office 2003
Novice
How to insert comments thro' macro
 
Join Date: Mar 2015
Location: Chennai, Tamilnadu, India
Posts: 7
AlexandarR is on a distinguished road
Smile How to insert comments thro' macro

It works excellent!!!

Thank you very much!!!
Reply With Quote
  #4  
Old 03-15-2015, 11:01 AM
AlexandarR AlexandarR is offline How to insert comments thro' macro Windows XP How to insert comments thro' macro Office 2003
Novice
How to insert comments thro' macro
 
Join Date: Mar 2015
Location: Chennai, Tamilnadu, India
Posts: 7
AlexandarR is on a distinguished road
Thumbs up Macro to find a text and format it, adding a comment

Quote:
Originally Posted by AlexandarR View Post
It works excellent!!!

Thank you very much!!!

Hi! This Alex again!

I have modified the code slightly, to change the colour and not to replace anything.

code:

Sub z_Abstract()
Dim vFindText As Variant
Dim sReplaceText As String
Dim oRng As Range
Dim oSearch As Range
Dim oFound As Range
Dim i As Long
Dim sAsk As String
Options.DefaultHighlightColorIndex = wdTurquoise
vFindText = Array("I", "we", "us", "our")
sReplaceText = "zzzzz"
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="\<ABS\>*\<\/ABS\>", MatchWildcards:=True)
Set oSearch = oRng
For i = 0 To UBound(vFindText)
Set oFound = oSearch
With oFound.Find
Selection.Font.Color = wdColorPink
.ClearFormatting
.Replacement.ClearFormatting
.MatchWholeWord = True
Do While .Execute(FindText:=vFindText(i), _
MatchWholeWord:=True, _
MatchWildcards:=True, _
Forward:=True, _
Wrap:=wdFindStop) = True
'oFound.Text = sReplaceText

oFound.HighlightColorIndex = wdTurquoise
oFound.Font.Color = wdColorPink
oFound.Comments.Add oFound, "CE: The use of personal pronouns (I, we, us, our) is not permitted in the abstract."
oFound.Collapse wdCollapseEnd
If oFound.End >= oSearch.End Then Exit Do
Loop
End With
Next i
Loop
End With
lbl_Exit:
Exit Sub
End Sub

That code finds in order of the array only. i.e. In the following text, the macro finds the text in array only in the same order and it finds a text once only.

<ABS>The use of personal pronouns (our, us, I, we) is not permitted in the abstract. The use of personal pronouns (I, we, us, our) is not permitted in the abstract.

I need to find all occurrences of a [whole] word irrespective of the order, within the coded text.

Please help.

Thanks!
Reply With Quote
  #5  
Old 06-05-2015, 10:38 PM
macropod's Avatar
macropod macropod is offline How to insert comments thro' macro Windows 7 64bit How to insert comments thro' macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can't use '.MatchWholeWord = True' when you're also using 'MatchWildcards:=True'; the latter overrides the former. You need to either:
(a) set 'MatchWildcards:=False'; or
(b) delete '.MatchWholeWord = True' and enclose each of the find expressions in a pair of chevrons (e.g. <I>).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
insert comment, vba macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert comments thro' macro How to Insert more criteria in Macro shilabrow Excel Programming 4 06-23-2014 08:16 AM
Macro to insert Text Morte Excel Programming 1 03-04-2014 04:33 PM
How to insert comments thro' macro Macro to insert picture in footer Sharon Word 5 01-29-2013 03:12 AM
Macro to insert new page... samanthaj Word 17 01-31-2012 01:53 PM
How to insert comments thro' macro Macro for Picture Insert rfhall50 Word VBA 2 10-25-2010 12:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:13 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft