Solved

Check if Word document is open using Visual Basic

Posted on 2008-09-29
4
349 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 81

Expert Comment

by:byundt
ID: 22598454
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
ID: 22598895
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
ID: 22599814
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
ID: 31501255
Thanks to all. This reply was what I was looking for.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

910 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

21 Experts available now in Live!

Get 1:1 Help Now