PDA

View Full Version : Solved: Check if a document is open



gibbo1715
12-16-2005, 04:47 AM
All

How do i check If a document is open and if it is close it?

Cheers

Gibbo

Bob Phillips
12-16-2005, 05:51 AM
Sub Test()
Dim oDoc As Document

On Error Resume Next
Set oDoc = Documents("Gibbo.doc")
On Error GoTo 0
If Not oDoc Is Nothing Then
oDoc.Close savechanges:=False
End If
End Sub

gibbo1715
12-16-2005, 06:49 AM
Thanks

Gibbo

gibbo1715
12-16-2005, 07:58 AM
Gave this a try using a string but causing an error of bad file name?

Any Ideas



StrApplication = "Q:\Gibbo.doc"
Set oDoc = Documents(StrApplication)
If Not StrApplication Is Nothing Then
StrApplication.Close savechanges:=False
End If



cheers

Gibbo

Bob Phillips
12-16-2005, 08:39 AM
Gave this a try using a string but causing an error of bad file name?

Any Ideas



StrApplication = "Q:\Gibbo.doc"
Set oDoc = Documents(StrApplication)
If Not StrApplication Is Nothing Then
StrApplication.Close savechanges:=False
End If



cheers

Gibbo

Drop the Q:\. If it is open the file is not identified by its drive.

gibbo1715
12-16-2005, 08:46 AM
Of course, makes more sense now and works fine but doesnt do exacly what im after.

I am trying the code as follows but it tells me I have a bad file name (My filename is correct)

Any ideas

Or even better is it possible to close all documents other than the active document where I identify the first 13 characters of the file name

Gibbo

StrApplication = Left(ActiveDocument.Name, 13) & ".doc"

Set oDoc = Documents(StrApplication)
On Error GoTo 0
If Not oDoc Is Nothing Then
oDoc.Close savechanges:=False
End If

gibbo1715
12-16-2005, 09:03 AM
I think my problem is in my document name as its all numbers as below


Any ideas

Gibbo

StrApplication = "051216142926.doc"

Documents(StrApplication).Close False

fumei
12-16-2005, 09:26 AM
Remember, you are actually trying to access the Documents Collection. If you want to close EVERYTHING except the current activedocument, then:

Sub CloseALLExceptActive()
Dim ThisDoc As Document
Dim DumpMe As Document
Set ThisDoc = ActiveDocument
For Each DumpMe In Application.Documents()
If DumpMe.Name <> ThisDoc.Name Then
DumpMe.Close wdDoNotSaveChanges
End If
Next
Set ThisDoc = Nothing
End Sub

gibbo1715
12-16-2005, 02:25 PM
Thanks i ll give that a go but im still curious why a document with a name made up of numbers causes an error bad file name?

Cheers

Gibbo

fumei
12-18-2005, 02:02 AM
This is a very old DOS error. While, true, current Windows theoretically contains no direct DOS instructions (it uses a new kernel), there are some vestiges. Another example is basic file attributes, although it now very rare to have any of those errors show up.

I would be curious if anyone out there using 64-bit Windows can duplicate this.

gibbo1715
12-18-2005, 02:31 AM
Oh well there you go then, that one caught me by suprise,

Ok Thanks

One to remember for the future

Gibbo

gibbo1715
12-18-2005, 03:21 AM
With your help found a way around my problem.

I have a number of documents that all start with a bunch of numbers 12345, 12345A
12345B etc

I needed to be able to click a button on 12345B whilst 12345a ( or c,d,e etc..) might (Or might not) be open.

I then needed to close those other documents and copy them to a new location

The code below does that for me

Gibbo

Sub CloseCode()
Dim ThisDoc As Document
Dim DumpMe As Document
Dim DocName As String
Dim Original As String

DocName = Left(ActiveDocument.Name, 5)
Set ThisDoc = ActiveDocument
For Each DumpMe In Application.Documents()
If Left(DumpMe.Name, 5) = DocName And DumpMe.Name <> ThisDoc.Name Then
Original = DumpMe.Path & "\" & DumpMe.Name
DumpMe.SaveAs "C:\" & DumpMe.Name
DumpMe.Close 'wdDoNotSaveChanges
Kill (Original)
End If
Next
Set ThisDoc = Nothing
Set Original = Nothing

End Sub