Solved

Check if Word document is open using Visual Basic

Posted on 2008-09-29
4
346 Views
Last Modified: 2012-05-05
Hi,

I have a Visual Basic application that combines several word documents into one. I found that there is an error when a word document is open by another user. How can I check to see if a word document is open before I do anything using visual basic?

Thanks
0
Comment
Question by:ALawrence007
4 Comments
 
LVL 80

Expert Comment

by:byundt
Comment Utility
You might try renaming the file and trapping the error.

On Error Resume Next
Err.Clear
Name "Some Path\" & "Some File.doc" As "Some Path\" & "Some File.doc"
If Err<>0 Then      'File is in use
Err.Clear
On Error GoTo 0
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
Comment Utility
See:
http://support.microsoft.com/kb/209189

Sub YourMacro()

   Dim strFileName As String

   ' Full path and name of file.

   strFileName = "C:\test.doc"

   ' Call function to test file lock.

   If Not FileLocked(strFileName) Then

      ' If the function returns False, open the document.

      Documents.Open strFileName

   End If

End Sub

Open in new window

0
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
Comment Utility
In VB.Net, you would use:

Private Function FileInUse(ByVal FileName As String)
Dim fs As IO.FileStream
Try
fs = New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Write, IO.FileShare.None)
Catch ex As Exception
Return True
End Try
fs.Close()
Return False
End Function
 
0
 

Author Closing Comment

by:ALawrence007
Comment Utility
Thanks to all. This reply was what I was looking for.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now