Open or BringToFront

L

Lars Brownies

In an Access2003 application I use the following code to open a
Word-document:

Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
oApp.Documents.Open FileName:=strFullPath
oApp.Activate
Set oApp = Nothing

When the doc is already opened, Word gives the user 3 choices (Open
read-only, etc.). I don't want this to happen. If the Word doc is already
opened by the particular user I just want Access to bring the already opened
doc to front.

Is this possible?

Thanks for any thoughts.

Lars
 
D

Douglas J. Steele

You'll need loop through the Documents collection and check whether the
file's already open or not. If it is, make it the ActiveDocument. If not,
open it.
 
L

Lars Brownies

Thanks Doug.
Regarding the loop I did some fiddling:

Dim oApp As Object
Dim objDocument As Object

Set oApp = CreateObject(Class:="Word.Application")
For Each objDocument In oApp.Documents
If objDocument.Name = "MyFile.doc" Then
debug.print "Found"
End If
Next objDocument

However, this only works if I have opened the word document, after the:
Set oApp = CreateObject(Class:="Word.Application")

If I open the file manually and then run the code, it won't notice that the
document is already open. How do I assign the already opened documents to
oApp.Documents?

Lars
 
D

Douglas J. Steele

Your code creates a new instance of Word. To use the one that's already
open, you need to use GetObject instead of CreateObject.

Try something like

Dim oApp As Object
Dim objDocument As Object
Dim booFound As Boolean

On Error Resume Next

Set oApp = GetObject(Class:="Word.Application")

If Err.Number = 429 Then
Set oApp = CreateObject(Class:="Word.Application")
Else
For Each objDocument In oApp.Documents
If objDocument.Name = "MyFile.doc" Then
booFound = True
Exit For
End If
Next objDocument
End If

If booFound = False Then
oApp.Documents.Open FileName:=strFullPath
End If
 
L

Lars Brownies

Thanks again. I got it working. I added:

oApp.Visible = True
oApp.Activate
Set oApp = Nothing

to bring the Word window to front and encountered a problem. If the doc was
already opened and minimized, it would not bring the word window to front.

I found the following code:
oApp.WindowState = wdWindowStateMaximize
but Access didn't recognize wdWindowStateMaximize. It thinks it's a
variable.

Then by chance I found the solution. I started fiddling with this parameter
with numbers. I found that
oApp.WindowState = 0
does the job.

Searching some more i found that this 0 equals vbNormal which ís recognized
by Access.

The last bit of code is now:
oApp.Visible = True
oApp.Activate
oApp.WindowState = vbNormal
Set oApp = Nothing

Lars
 
L

Lars Brownies

One more question about the 'error resume next' line. Can I use 'on error
goto errhandler' later on in the same procedure to do my regular error
handling?

For instance: with error resume next, an error in the following code:
oApp.Documents.Open FileName:=strFullPath
won't trigger an error.

Thanks.
Lars
 
D

Douglas J. Steele

Yes you can.

Another alternative would be to use On Error GoTo ErrHandler from the
get-go, and change your error handler to treat the 429 error appropriately:

ErrHandler:
Select Case Err.Number
Case 429
Set oApp = CreateObject(Class:="Word.Application")
Resume Next
Case Else
MsgBox ...
Resume ....
End Select
 
D

Douglas J. Steele

You're using Late Binding, which means you don't have a reference set to the
Word object library. Therefore, intrinsic Word constants like
wdWindowStateMaximize are unknown to Access.

Define the variable to Access, and you'll be fine:

Const wdWindowStateMaximize As Long = 1

(to find out the value, open Word, go into the VB Editor in Word and type
?wdWindowStateMaximize in the Immediate Window)
 

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