Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

find text and replace with docvariable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Diane - 09 Nov 2006 19:28 GMT
Group,
I am testing code that will find text such as "##" and replace it with a
docvariable.
I can successfully read a directory of over 500 MSWORD documents and find
text and replace text, but I need to find text and replace with a docvariable
field.

I want to add this code for my "replacement" but not having any luck doing
this:

  "Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
       "DOCVARIABLE  abc ", PreserveFormatting:=True
       Selection.Font.color = wdColorDarkRed
     
My code that works with find/replace text :

With oWord.Selection.Find
           .ClearFormatting()
           .Text = "##"
           
           If oWord.Selection.Find.Found = True Then
               Numberoffiles += 1
           End If

           With .Replacement
               .ClearFormatting()
               .Text = "**TEST**"
               .Font.Color = Word.WdColor.wdColorRed
           End With

           .Execute(Replace:=Word.WdReplace.wdReplaceAll)

       End With

I am coding a VB.NET application with a MSWORD template.doc.
Using MSOFFICE 2003.
Diane
Greg Maxey - 09 Nov 2006 19:49 GMT
Something like this perhaps:
Sub ScratchMacro()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
 .Text = "##"
 While .Execute
   oRng.Delete
   oRng.Fields.Add oRng, wdFieldDocVariable, """Your Variable Name""",
True
 Wend
End With
End Sub

> Group,
> I am testing code that will find text such as "##" and replace it with a
[quoted text clipped - 33 lines]
> Using MSOFFICE 2003.
> Diane
Diane - 11 Nov 2006 14:33 GMT
Greg,
Many thanks for your example.  I finally had a chance to try this and it is
almost working, I'm stuck in a loop,  I'm not clear as to how to have it move
on to my next find, it is finding my first text, but then it repeats the
while loop.  My end result, it replaces only the first "*" and adds many
docvariables at that same position.


 orng = oWord.ActiveDocument.Range

       With orng.Find
           .ClearFormatting()
           .Text = "*"
Loop-->            While .Execute
                         orng.Delete()
                          orng.Fields.Add(orng,
Word.WdFieldType.wdFieldDocVariable, "abc", True)
                        End While

       End With

Thanks,
Diane
 

> Something like this perhaps:
> Sub ScratchMacro()
[quoted text clipped - 47 lines]
> > Using MSOFFICE 2003.
> > Diane
Diane - 11 Nov 2006 19:28 GMT
Thank you Greg, found my problem, your example was great!!
Diane

> Something like this perhaps:
> Sub ScratchMacro()
[quoted text clipped - 47 lines]
> > Using MSOFFICE 2003.
> > Diane
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.