Hiding open document

M

macroapa

Hi, is it possible to write code that hides the open word document
from view (but doesn't actually close it) and vice versa.

I know you can hide workbooks in excel, but i cant see that you can do
the same in Word so cant even begin to try and code it.

Cheers.
 
J

Jean-Guy Marcil

macroapa said:
Hi, is it possible to write code that hides the open word document
from view (but doesn't actually close it) and vice versa.

I know you can hide workbooks in excel, but i cant see that you can do
the same in Word so cant even begin to try and code it.

Here is some code to get you going...

Sub HideDoc()

ActiveDocument.ActiveWindow.Visible = False

End Sub

Sub resetHiddenDoc()

Dim myDoc As Document

For Each myDoc In Application.Documents
If myDoc.ActiveWindow.Visible = False Then myDoc.ActiveWindow.Visible =
True
Next

End Sub
 
M

macroapa

Here is some code to get you going...

Sub HideDoc()

ActiveDocument.ActiveWindow.Visible = False

End Sub

Sub resetHiddenDoc()

Dim myDoc As Document

For Each myDoc In Application.Documents
    If myDoc.ActiveWindow.Visible = False Then myDoc.ActiveWindow.Visible =
True
Next

End Sub

thanks for that, it's not quite doing what i want thou. The code i
will be using based on what you have said is:

Documents("xyz.dot").activewindow.visible = false
Documents.open ("abc.doc")

however, this leaves abc.doc also hidden. What I need is xyz,dot to be
hidden and abc.doc remain viewable, this is so i can easily debug the
code that actually sits in xyz.dot without it getting in the way of
what the code is doing (if i leave xyz.dot open, then my code can end
up putting text in xyz.dot instead of abc.doc)

cheers.
 
G

Gordon Bentley-Mix

If I understand your post correctly, the problem is probably best solved not
by trying to hide a document but rather by restructuring your code so you
make specific reference to a particular document. These sorts of problems
come up when using the ActiveDocument object and stepping code to debug it.
Word recognises the ActiveDocument as the one that has focus. However, if you
create a new Document object and set that object to refer to a specific
document ("abc.doc" in your example) then write your code against the object,
the problem goes away.

The basic structure of the code is something along the lines of the following:

Sub Test()
Dim myDoc As Document
Dim Message As String
Set myDoc = Documents.Open("doc17.doc")
'*** Do stuff with myDoc; e.g. display a count of the tables in the
document ***
With myDoc
Select Case .Tables.Count
Case 0
Message = "There are no tables in this document."
Case 1
Message = "There is 1 table in this document."
Case Else
Message = "There are " & myDoc.Tables.Count & " tables in this
document."
End Select
MsgBox Message
myDoc.Close wdDoNotSaveChanges
End With
Set myDoc = Nothing
End Sub

Obviously this is a greatly simplified example (and not very useful because
I can't imagine why anybody would want to open a document, display a count of
the tables in it and then just close it again - but it worked for a
proof-of-concept using what I had easily available on my machine). However,
it should give you an idea of how to restructure your code to ensure that you
don't bugger up your template when you're developing.

Plus this makes a lot more sense than writing some 'throw-away' code for
debugging purposes only; any code you write against the specific document
object will work equally well in production. And it's just good practice to
refer to specific instances of objects rather than relying on something as
generic as the ActiveDocument or a UserForm class (yes Jean-Guy? ;-D).
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

Apologies for the extraneous 'myDoc' in the line:

myDoc.Close wdDoNotSaveChanges

In my haste to put together my POC, I missed the fact that I was still
within the bounds of the With block. The line should be:

.Close wdDoNotSaveChanges
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
J

Jean-Guy Marcil

macroapa said:
thanks for that, it's not quite doing what i want thou. The code i
will be using based on what you have said is:

Documents("xyz.dot").activewindow.visible = false
Documents.open ("abc.doc")

however, this leaves abc.doc also hidden. What I need is xyz,dot to be
hidden and abc.doc remain viewable, this is so i can easily debug the

Open abc.doc before hiding xyz.dot...
code that actually sits in xyz.dot without it getting in the way of
what the code is doing (if i leave xyz.dot open, then my code can end
up putting text in xyz.dot instead of abc.doc)

....however, I will second Gordon on this.
If you worry that code in a project might act on the wrong document, then
you need to review your code.
Anyway, the code might still act on the wrong document if it is merely
hidden...
Use Document objects to get a handle on the appropriate document and avoid
using the Selection opbject.
 

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