If a sentence is in the document then...

  • Thread starter Guus van Waardenburg
  • Start date
G

Guus van Waardenburg

Hi All,

I'm having te following problem:

I want my code to look if a sentence does excist and if not then type the
sentence on the end of my document. I can't seem to figure out how to do
this. Can anyone help me?
 
H

Helmut Weber

Hi Guus,

how about this,
for a very simple document:

Sub Macro3()
Dim rDcm As Range
Dim sTmp As String
sTmp = "The quick brown fox jumps over the lazy dog."
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = sTmp
If Not .Execute Then
rDcm.InsertAfter " " & sTmp
End If
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg Maxey

Something like this should do:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim pStr As String
Set oRng = ActiveDocument.Range
pStr = "Your sentence text."
With oRng.Find
.Text = pStr
.Execute
If .Found = False Then
oRng.InsertAfter " " & pStr
End If
End With
End Sub
 

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