PDA

View Full Version : check if doc is open



saban
11-08-2007, 10:19 AM
How do I check if any document in word is open and if not open one

TonyJollans
11-08-2007, 10:21 AM
Do you mean check if a specific document is open, or just any document at all?

saban
11-08-2007, 10:49 AM
I think the best way would be if Document1 is open close it, and prompt for saving if not empty

TonyJollans
11-08-2007, 11:56 AM
Quick reply at the moment as I'm not sure I fully understand ...

This should close all open documents and Word will prompt for any that need saving

while documents.count > zero
activedocument.close
wend


and this will give you your document to work on


set mydoc = documents.add
' run your stuff
mydoc.close

saban
11-08-2007, 01:07 PM
No I must not close other documents, just Document1

TonyJollans
11-08-2007, 01:26 PM
As you've effectively removed your earlier post I'm getting more confused.

That doesn't really make sense - there is no situation I know where that is a guarantee of anything, or even likely to be helpful. Can you give a bit more detail of what you're doing.

saban
11-08-2007, 01:49 PM
There appears to be some bug in MSword at least for my macro which deals a little with xml, I dont know why is this but if:

Document1 is open (by default it opens when you open word) the macro throws an error "path access error" something like that and it turns out that with Document1 closed it works as it should.
Even If you open another which name will be Document2 it works OK it is just with Document1 that it doesnt

It is very strange

I need to look amongst open documents if some of them is Document1 and close it
(That is all I need)

TonyJollans
11-08-2007, 01:56 PM
I'd be interested to see the code that causes the problem but this will do what you ask..

For Each Doc In Documents
If Doc.Name = "Document1" Then Doc.Close
Next

saban
11-08-2007, 02:02 PM
It si OK I figured this

Dim wDoc As Window
' Loop through all Windows currently open.
For Each wDoc In Windows
' Display name of document open in the current window.
If wDoc.Document.Name = "Document1" Then
wDoc.Close
End If
Next

Thnx

saban
11-08-2007, 02:04 PM
I didnt see that you already answered :)

I will send you the code for you to look at (tomorrow)

Stay cool
Saban

fumei
11-09-2007, 01:55 PM
It would be better to go through the Documents (Tony's code) than the Windows (your code). For one thing, your wDoc.Close closes the window, as that is what it is. You would get prompted regarding the document. Tony's code deals with the Document, which is what you want.

saban
11-09-2007, 04:11 PM
Thnx fumei I realize that when it threw me an error when tying to add my code to button when form was already shown("you cannot close window if dialog is open") :)

Thnx

fumei
11-10-2007, 10:40 PM
Yes, it would. SOMETHING has to have focus.

Generally speaking, it is better to deal with the actual things you want to deal with.

In this example, the Window really has nothing at all to do with what you want to deal with. You want to deal with Documents? Then deal with Documents.

This is also why when dealing with "chunks" of a document it is better to use Range, than Selection. For a similar reason. You want to deal with chunks (ranges) of a document, then use Ranges. Selecting things has, literally, nothing to do with actioning parts of a document.

EXCEPT visually, manually, within the GUI.

One of the main points of using code (VBA) is that you NOT have to use the GUI.

So, when you don't have to...why do it?

Compare the following two code windows. They both do the exact same thing. EXACTLY the same results.Sub TheLongWay()
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView _
Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With ActiveDocument.Styles(wdStyleNormal).Font
If .NameFarEast = .NameAscii Then
.NameAscii = ""
End If
.NameFarEast = ""
End With
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1.7)
.RightMargin = InchesToPoints(2)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = True
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With
ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.TypeText Text:= _
"This is second page Header."
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
ActiveWindow.ActivePane.View. _
PreviousHeaderFooter
Selection.TypeText Text:="This is First page footer"
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

And the short way.
Sub OtherWay()
With ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Range.Text = _
"This is second page Header."
.Footers(wdHeaderFooterFirstPage).Range.Text = _
"This is First page footer"
End With
End SubBy dealing with, and actioning only, the things you really DO want to action, you can make your actions clean, clear, and best of all...easily understood. There is nothing worse than looking at (your own) old code and scratching your head, going...ummmmm, "huh"?

Comments help of course.