Help with Resetting Focus to a Previous Document

U

Umpty

Summary: I am working from within MS Access. I open "DocumentA", read a
column of values (one at a time). For each read, I go to "DocumentB" and
walk down a column of values to see if that value is equal to the value that
was read from "DocumentA". If it is, it sets a hyperlink, referencing a
bookmark in "DocumentA".

Problem is: It does this fine on the first iteration but when it goes back
to the code for "DocumentA", the active document focus is on "Document B" and
I need to get the focus back on "Document A". I have tried to use code such
as:
Documents.("DocumentB.doc").Activate but that does not work. I get an
error that says "Bad file name".

Any help in reseting the focus back on "DocumentB.doc" would be greatly
appreciated.


' This code opens "DocumentA.doc" and does what it needs (only on the first
iteration).

Dim TotRows As Integer
Set oApp = New Word.Application
oApp.Documents.Open CurrentProject.Path & "\DocumentB.doc"
oApp.Visible = True
TotRows = oApp.ActiveDocument.Tables(strTableID).Rows.Count
Dim i As Integer

For i = 1 To TotRows
Dim TotLen As Integer, MyText As String, MyBkmrk As String
oApp.ActiveDocument.Tables(strTableID).Rows(i).Cells(2).Select
TotLen = Len(oApp.Selection.Text) - 2
MyText = Trim(Left(oApp.Selection.Range, TotLen))
MyBkmrk = strBkMrkPrefix & i
OpenPicklistFile strTableID, MyText, MyBkmrk
Next i

'This code opens Document B and does it's thing and goes back to previous
code to do the next read.

oApp.Documents.Open CurrentProject.Path & "\DocumentB.doc"
oApp.Visible = True
TotTables = oApp.ActiveDocument.Tables.Count
For j = 1 To TotTables
TotRows = oApp.ActiveDocument.Tables(j).Rows.Count
For i = 3 To TotRows

oApp.ActiveDocument.Tables(j).Rows(i).Cells(MyColumnNum).Range.Select
TotLen = Len(oApp.Selection.Text) - 2
myValue = Trim(Left(oApp.Selection.Range, TotLen))
If myValue = strMyText Then
oApp.Selection.Range.Delete
oApp.ActiveDocument.Hyperlinks.Add
Anchor:=oApp.Selection.Range, Address:= _
"DocumentA.doc", SubAddress:=strBkMrk, _
ScreenTip:="", TextToDisplay:=myValue
End If
Next i
Next j
 
P

Pete Bennett

An easier solution might be to declare objects as documents...

Dim aDoc as Word.Document
Dim bDoc as Word.Document

Set aDoc=Word.Documents.Open("filenamea.doc")
Set bDoc=Word.Documents.Open("filenameb.doc")

Then you can iterate the documents by their pointers, rather than trying to
activate them.

For each atable in adoc.tables
.....
Next aTable

That way, you can open up the document you're going to read the information
from (and call it aDoc) and then open the document you'll be putting the
information into (bDoc). And then you won't have to worry about flipping
between the two.

But if you do, it'll just be aDoc.Activate or bDoc.Activate
 

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