Check if ActiveDocument Exists

L

LostInMD

Hi,

I'm attempting to determine whether there is an ActiveDocument off the
Application.

Scenario: Word application is running but there is no open document. User
clicks a commandbar button. I need to determine in the code behind the
button if there is an ActiveDocument for me to type text onto. Cannot do a
Application.Documents.Count b/c it sometimes returns a hit for the Normal.dot
template. I need to determine whether there is an active .doc.

Thanks!
 
J

Jean-Guy Marcil

LostInMD was telling us:
LostInMD nous racontait que :
Hi,

I'm attempting to determine whether there is an ActiveDocument off the
Application.

Scenario: Word application is running but there is no open document.
User clicks a commandbar button. I need to determine in the code
behind the button if there is an ActiveDocument for me to type text
onto. Cannot do a Application.Documents.Count b/c it sometimes
returns a hit for the Normal.dot template. I need to determine
whether there is an active .doc.

Thanks!

Use something like this:

If Documents.Count > 0 Then
MsgBox ActiveDocument.Name
Else
MsgBox "No documents opened."
End If

If you get a pop up stating "Normal.dot" than it means that your normal.dot
is opened...

I tried various things, and I never got the pop up to state "Normal.dot"
unless it was actually opened.

If you still get that weird result, show us your code.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Steve Yandl

You could probably use the Tasks collection. Each task has both a 'Name'
property and a 'Visible' property. You could loop through the collection,
check if the right 14 characters in the string returned by the name property
were "Microsoft Word" and whenever you found such an instance, check if the
'Visible' property is true.

Steve
 
L

LostInMD

Thanks for the quick reply Jean-Guy :)

In testing your suggestion, I create the environment that causes the quirk,
then:
In the immediate window I try -->
?Application.Documents.Count --> and get "1"
Then I try -->
?Application.ActiveDocument.Name --> and get "This command is not available
because no document is open."

Following are the steps and code that create my scenario. I start the Word
exe. I close the blank form that is loaded during the above step. I now
have a Word application with no open document (gray background). I then
click a command button with the following code behind it:

Application.KeyBindings.ClearAll()

Prior to my code hitting this line, in my immediate window I try -->
?Application.Documents.Count --> and get 0. Then I try -->
?Application.Windows.Count --> and get 0. Then I run the line of code
above. After the one line of above code is run, in my immediate window I try
-->
?Application.Documents.Count --> and get 1. Then I try -->
?Application.Windows.Count --> and get 0. Then I try -->
?Application.Documents(1).Name --> and get "Normal.dot".

Please let me know if the KeyBindings line of code creates the same results
for you in terms of the documents count. Thank you for all your help! Any
suggestions are greatly appreciated :)
 
L

LostInMD

Hi Steve,

Thank you very much for your suggestion. When I attempted to test your
method, I got a Tasks collection of 100+ items. I checked each item for the
name and found 2 "Microsoft Word" and both had the 'Visible' property = true.
(All the while the Documents.Count = 1 and the Windows.Count = 0). If you
don't mind, please try the scenario in my reply to Jean-Guy Marcil (located
right above your reply) and return feedback. Any assistance would be greatly
appreciated!

Thanks!
 
J

Jean-Guy Marcil

LostInMD was telling us:
LostInMD nous racontait que :
Thanks for the quick reply Jean-Guy :)

In testing your suggestion, I create the environment that causes the
quirk, then:
In the immediate window I try -->
?Application.Documents.Count --> and get "1"
Then I try -->
?Application.ActiveDocument.Name --> and get "This command is not
available because no document is open."

Following are the steps and code that create my scenario. I start
the Word exe. I close the blank form that is loaded during the above
step. I now have a Word application with no open document (gray
background). I then click a command button with the following code
behind it:

Application.KeyBindings.ClearAll()

Prior to my code hitting this line, in my immediate window I try -->
?Application.Documents.Count --> and get 0. Then I try -->
?Application.Windows.Count --> and get 0. Then I run the line of
code above. After the one line of above code is run, in my immediate
window I try -->
?Application.Documents.Count --> and get 1. Then I try -->
?Application.Windows.Count --> and get 0. Then I try -->
?Application.Documents(1).Name --> and get "Normal.dot".

Please let me know if the KeyBindings line of code creates the same
results for you in terms of the documents count. Thank you for all
your help! Any suggestions are greatly appreciated :)

I cannot reproduce.

Documents.Count is always 0, even after creating a button in my Normal.dot
that would call the keybinding code and following you scenario.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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