how to set current document as active document

Hi,

I have a function that sorts through and selects text in a word document.  It works fine as a stand alone function, but i want to select multiple sections and not have to open and close the document over and over again.

I think i need to set the document at the point in the code i have marked Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'*****  This is where i just want it to use the current open document

Any ideas?
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'*****  This is where i just want it to use the current open document
 
Set objDoc = 
objWord.Documents.Open("E:\_epiq\masterDocs\TestCopy1.doc")
Set objSelection = objWord.Selection

Dim WildCard,Firstpart,Secondpart

Fistpart="\[section\]"
Secondpart="*\[/section\]"
WildCard=Firstpart & Secondpart

objSelection.Find.Forward = True
objSelection.Find.MatchWildcards = True
objSelection.Find.Text = WildCard

Do While True
    objSelection.Find.Execute
    If objSelection.Find.Found Then
        strWord = objSelection.Text
        strWord = Replace(strWord, "[section]", "")
        strWord = Replace(strWord, "[/section]", "")
        'Wscript.Echo strWord
    Else
        Exit Do
    End If

Loop

Open in new window

PrimedWebbieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Set objDoc = objWord.ActiveDocument
0
Sarika30Commented:
Are you trying to read from different open docs?
try using
Set objDoc = .ActiveDocument
0
PrimedWebbieAuthor Commented:
Neither of those work sorry.  the command is not available because no document is open is the error but it opens up the document and i can see it on the screen.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
>>> This is where i just want it to use the current open document

Since you have just tries to create objWord  this can only work if word is already open and you want to connect to that instance.  If so then try replacing:

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'*****  This is where i just want it to use the current open document
Set objDoc =

with

Set objWord = GetObject(, "Word.Application")
objWord.Visible = True
Set objDoc = objword.documents("mydocumentname")

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PrimedWebbieAuthor Commented:
found another solution but thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.