Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this document open? 2

Status
Not open for further replies.

Cloonalt

Programmer
Jan 4, 2003
354
US
I pass a variable to the below to check if a document is open. If it's open, fine; however if it's not open, I get an error. I can trap the error, but is there a better way to check if there's a specific document already open?

If ActiveDocument = Request & ".doc" Then....

Thanks.

Any help appreciated.
 
Here's what happens. Maybe someone will get a clue. I can't see the forrest for the trees maybe.

I open my application. Click on a button and launch Word and then create a document in code.

If I try to create the document again, my application sees that its the current document already and I can put up an error.

BUT!! With my app still open, if I close the document and Word, then click and create the document again, and then do it a second time, my app doesn't see Word or the document and tries to create it a second time. I can trap that error when Word tries to save the same document a second time, but meanwhile I've created the same document - one named correctly and one named 'Document1'.

I'm just trying to make this as clean as possible for the user.

Any help is appreciated.
 
Clearly there is something wonky with your instances of Word. Could you post exactly how you are creating, closing, destroying, your instances?

Gerry
 

The behaviour of these things can be release dependent but I suspect you are maintaining a pointer to the first instance of Word (possibly one that has been implicitly created) which is, therefore, not properly terminated, and then creating a second instance and not always addressing the instance you think you are.

Can you provide what Gerry asks for, and also details of all references to either Word or any Documents or other Word objects.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Here's the whole thing. I want the document to open and stay open until the user closes it. However, it that's a problem, I'll close it, kill Word and give the user a message about where it is.

Thanks for your help!!



Private Sub cmdCompileBatches_Click()

'_______________________________________________________________________
'
'Allows user to merge all batches for a CC number into one Word document
'_________________________________________________________________________

On Error GoTo ErrorHandler

Dim rs As DBResults
Dim strSQL As String
Dim Batch As Long
Dim Request As String
Dim oapp As Word.Application
Dim SearchFolder As String
Dim FirstDocument As String
Dim psindex As Long
Dim doc As String
Dim DestinationFolder As String
Dim ActiveDocument As String

'Save the document

DestinationFolder = "//phlactgfp01/conflicts/"
SearchFolder = "//phlactgfp01/conflicts/"

'Get the Batch number entered by the user
Batch = Me.txtBatchID
Request = Me.txtSearchDesc.Text

'Check to see if the document is already open. If it is prompt user and stop
If IsDocOpen(Request & ".doc") = True Then
MsgBox "Word document " & Request & Chr(13) & Chr(10) & "is open. Please close.", vbCritical, "Document Exists and is Open"
Exit Sub
End If

'Open Word, set it to visible and minimize
Set oapp = CreateObject("Word.Application")
oapp.Visible = True
oapp.Application.WindowState = wdWindowStateMinimize

strSQL = "Select psindex, psdesc from psearch where psdesc = " & "'" & Request & "'" & _
" ORDER BY psindex asc"

Set rs = Application.DB.OpenResults(strSQL)

If Not rs.AtEnd Then
With rs
FirstDocument = SearchFolder & Batch & "001.doc"
If FirstDocument = "" Then
'File does not exist
MsgBox (FirstDocument & " does not exist!"), vbCritical + vbOKOnly, "EIS Forms"
Exit Sub
End If

If vbNo = MsgBox("Merge All Batch Files?", vbYesNo + vbQuestion) Then
Exit Sub
Else
oapp.Documents.Add DocumentType:=wdNewBlankDocument
Do While Not rs.AtEnd
Batch = rs.Value(psindex)

'Find the latest document number for each batch
Dim f As String
doc = LCase(Batch & "001.doc")
f = LCase(Dir(SearchFolder & Batch & "???.doc"))
While f <> ""
If f > doc Then doc = f
f = LCase(Dir)
Wend
oapp.Selection.InsertFile SearchFolder & doc
oapp.Selection.InsertBreak
rs.MoveNext
Loop
oapp.ActiveDocument.SaveAs (DestinationFolder & Request & ".doc")
End If
End With

MsgBox vbCr & _
"Conflict of Interest Report " & vbCr & _
"Compiled in Word as " & Request & ".doc", vbOKOnly, "Conflict Report"
End If

'Cleanup
DestinationFolder = ""
SearchFolder = ""
Batch = 0
Request = ""
strSQL = ""
Set rs = Nothing


Exit Sub

ErrorHandler:

Select Case Err.Number
Case 5356
MsgBox "To save again, close document " & Request & "." & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Then click Compile Word Document.", vbCritical, "Document Exists and is Open"
Case Else
MsgBox "Error " & Err.Number & vbCr & _
"Location: " & "VBA.frmBatch.txtBatchID" & vbCr & _
Err.Description, vbOKOnly + vbExclamation, _
"Custom Conflicts Search"

End Select


End Sub

Function IsDocOpen(docName As String) As Boolean

On Error Resume Next

IsDocOpen = Len(Documents(docName).Name)
If IsDocOpen = True Then
End If

End Function
 
Hi Cloonalt,

A couple of points ..

First thing I'd say (and this may be the root of your problem) is to make sure all references to Word objects are properly qualified - for example Documents should be oapp.Documents, and not to check if your document is open before you've instantiated Word.

Also I don't know what you intend here but, early on you do
Code:
SearchFolder = "//phlactgfp01/conflicts/"
and then you do
Code:
 FirstDocument = SearchFolder & Batch & "001.doc"
            If FirstDocument = "" Then
                'File does not exist
                MsgBox (FirstDocument & " does not exist!"), vbCritical + vbOKOnly, "EIS Forms"
                Exit Sub
            End If
As FirstDocument can never be "" you may as well remove that code.

Finally, if Word may be open when you run this you may want to use GetObject rather than CreateObject.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 

Hi Cloonalt,

By a strange coincidence, I have just experienced the problem you are having. Running Word 2000 on Windows 2000 if it's relevant, and the cause was a missing reference in Normal.dot. In the VBE, select a module in Normal and check under Tools > References and see if any references are listeed as "MISSING"; if so, remove or correct them.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Thanks for that information Tony. We're running Windows XP and Word 2003.

My references in Normal.dot are Visual Basic for Applications, Mircosoft Word 11.0 Object Library, OLE Automation, Microsoft Office 11.0 Object Library. No missing references.

But the Normal.dot is very highly customized here, so what you're suggesting could be a good clue.

I'd like to know how to check to see if Word is open.

Fran
 
Hi Fran,

Sorry that didn't help but when it happened to me, I just had to post :)

There's no direct way to tell if Word is open. Best you can do is ask for a reference to a running instance ...
Code:
[blue]Function WordIsRunning() As Boolean
    Dim WordApp As Object
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    On Error GoTo 0
    WordIsRunning = Not WordApp Is Nothing
    Set WordApp = Nothing
End Function[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top