VBA Question

J

josus

I'm looking for VBA source code (using Ms-Word) that would would allow
me to:

1- Initialize a variable (ie. "replacement-text")
2- assign content of highlighted text in variable "replacement-text"
3- replace (Ctrl-H) regular text (ie "searched-text") with content of
variable "replacement-text"

Pseudocode would be something like this:
--Initialize a variable called "replacement-text"
--put content of hightlighted text in variable "replacement-text"
--replace all occurrences of the expression "searched-text" with the
content of variable "replacement-text".

Thanks.
 
H

Helmut Weber

Hi Josus,

how to do it, is a question of programming style.
Just a bit of sample code:

Sub Test77569()
Dim rDcm As Range
Dim sHlg As String ' highlighted text
Dim sFnd As String ' the text to be found
sFnd = "brown"
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Highlight = True
If .Execute Then
sHlg = rDcm.Text
End If
End With
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = sFnd
.Highlight = False
.Replacement.Text = sHlg
.Execute Replace:=wdReplaceAll
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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