Macro for finding a textline

  • Thread starter Vegard H. Villmones
  • Start date
V

Vegard H. Villmones

Hi.

I'm working in access, and I want to open a doc in word, alter the
properties of some of the text there. These lines are where it doesn't work:

objWord.Selection.Find.Execute FindText:="Etter talen"
objWord.Selection.Font.Name = rs!overskrifterType
objWord.Selection.Font.size = rs!overskrifterStr

should find the string "Etter talen", right? but it doesn't, it finds the
last string I entered into the FindText-value. Any obvious reason why this
is happening?

Vegard
 
P

Peter Hewett

Hi Vegard H. Villmones

Word's Find object can be flakey, so I always initialise the entire object. It's much
more stable this way. Try the following code:

Public Sub FindFirst()
Dim rngReplace As Word.Range

Set rngReplace = objWord.ActiveDocument.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Etter talen"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

If .Execute Then
MsgBox "Found"

' Use the rngReplace object here, not the selection object
With rngReplace.Font
.Name = rs!overskrifterType
.Size = rs!overskrifterSt
End With
Else
MsgBox "not found"
End If
End With
End Sub

I've not defined the record set.

HTH + Cheers - Peter
 

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