Open doc will not Activate.

R

Robert

Dear Experts, The macro below (Word 2002) is intended to open a doc
(if it is not already open) and to Activate it. The macro succeeds in
Opening the document but then fails to Activate it unless there is
another (unconnected) document in the Collection. If the only other
document is something like "Document1" then Activate does not work.
No error is generated. This has got me stumped. Help please. Robert.

Dim doc As Document
Dim docFound As Boolean

docFound = False

' Search all open docs for the one required.
For Each doc In Documents
If doc.Name = "Temp_Copy.doc" Then docFound = True
Next doc

If docFound = False Then
Documents.Open FileName:="C:\Documents and Settings\Patton\My
Documents\Temp_Copy.doc"
Else
Documents("Temp_Copy.doc").Activate
End If

WordBasic.EditOfficeClipboard

End Sub
 
S

Stefan Blom

I can reproduce this in Word 2003 as well. The following seems to work,
though:

For Each doc In Documents
If InStr(doc.FullName, "Temp_Copy.doc") Then
docFound = True
Exit For
End If

Next doc

' rest of code follows...

In other words, I'm using the InStr function to locate the file name *and*
I'm using If Then--End If.
 
R

Robert

Dear Stefan, Thanks a lot for your reply. Your modification now opens
and activates Temp_Copy.doc reliably but I still cannot on other
occasions get the already open document to activate. When I try, the
currently active document "freezes", losing its flashing cursor, and
has to be revived by minimizing and reopening. It looks as though the
'Activate' command is only partially completed. Yet I cannot see
anything wrong with the code (which is as shown below).

FWIW, the checkbox "Windows in taskbar" at Tools | Options | View |
has been both set and unset in my trials, but seems to make little
difference either way. Also I have tried removing the WordBasic
command to open the Clipboard, but that too did not solve the problem.

Thank you for your interest.
Robert.

Dim doc As Document
Dim docFound As Boolean

docFound = False
' Search all open docs for the one required.

For Each doc In Documents
If InStr(doc.FullName, "Temp_Copy.doc") Then
docFound = True
Exit For
End If

Next doc

MsgBox ("docFound = " & docFound)

If docFound = False Then
Documents.Open FileName:="C:\Documents and Settings\Patton\My
Documents\Temp_Copy.doc"
Else
Documents("Temp_Copy.doc").Activate
End If

WordBasic.EditOfficeClipboard

End Sub
 
S

Stefan Blom

Have you tried using the full path and name instead of just the document
name when accessing your document via the Documents collection? Does that
make a difference?

Also, have you been able to identify the situation where Activate fails?
Does it involve a particular number of open documents, similar file names,
or what?
 
R

Robert

Dear Stefan,
Thank you for your reply. I tried using the Full Pathname in
the .Activate line but it seemed to make no difference. I also tried
adding various combinations of files to the Documents Collection but,
as before, the pattern was inconsistent. Sometimes it worked;
sometimes it didn't under identical conditions. THEN I noticed
something....

During repeated opening and closing of the target file for testing, I
usually minimised it by using the Window Minimize button. But, if I
minimised the open target file by opening another file from the
Taskbar, on running the Macro again it invariably activated
Temp_Copy.doc correctly.

I have always thought that using the Window Minimize button was a
legitimate way to close a file to the Taskbar. Is this not so?

Since this discovery I have found that the combination of files in the
Documents Collection has no bearing on the issue at all. Activation
depends purely on how the Temp_Copy.doc was last closed. If from the
Taskbar, the Macro works every time; if from the Window controls, Word
temporarily "hangs". Have you any explanation for this?

Thank you for your patience and helpful advice. At least I have a work-
around now, but I do not yet understand Why.

Best Wishes,
Robert.
 
S

Stefan Blom

Interesting observation. Unfortunately, I have no explanation for the
behavior.
 

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