Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check if Word document is open using Visual Basic

Posted on 2008-09-29
4
Medium Priority
?
414 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
[X]
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
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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

721 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