Solved

Check if Word document is open using Visual Basic

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

770 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