Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Check if Word document is open using Visual Basic

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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:
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

791 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