Compile Error: Function call on left-hand side of assignment must return Variant or Object

Have created a table in Access with list of .doc files from certain directory as my recordset, am using For-Next to open each document.  This is working so far.  I am needing to find and replace in the document, and am getting the compile error.  PLEASE HELP!!

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim WordRange As Word.Range

Set objWord = New Word.Application
With objWord
.Visible = True
Set objDoc = .Documents.Open(strFile, , False)
End With

Set WordRange = objDoc.Content
With WordRange.Find
.Execute(FindText:=strTrust, REPLACEWITH:=Underscore, Replace:=Word.wdReplaceAll, Forward:=True) = True
End With
Stacey_ElaineAsked:
Who is Participating?
 
carsRSTCommented:
Since the execute statement returns a value (true/false), it's expecting you to evaluate it.

Honestly, you don't have to do anything with "blnVal".  Just put it there so you can execute the code.

you could, if you wanted, trap and see if anything was replaced and write code to handle hit.  But not necessary for your purposes.  True would mean it replaced; false would mean no replace.  

0
 
carsRSTCommented:
Did you set a reference to WORD?  It's a requirement to use the Word API.
0
 
carsRSTCommented:
Also, the ".execute" returns a boolean value so you must code for that.


something like this...

    If .Execute(FindText:=strTrust, REPLACEWITH:=Underscore, Replace:=Word.wdReplaceAll, Forward:=True) = True Then
   
    End If
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
carsRSTCommented:
yeah, either do an IF statement or assign a value to the .execute statement.

blnVal = .Execute(FindText:=strTrust, REPLACEWITH:=Underscore, Replace:=Word.wdReplaceAll, Forward:=True)
0
 
Stacey_ElaineAuthor Commented:
then what would i do with the bInVal? i know i sound like a kindergartner here, but ALL i want is to open a document, find and replace text! i'm able to find text and format, but for some reason not replace text. i'm lost. and not, clearly, a programmer.
0
 
Stacey_ElaineAuthor Commented:
blnVal worked! Thank you so much!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.