Opening word document

Posted on 2004-09-08
Last Modified: 2008-03-03

I open a word document in my program using
Dim oWord As New Word.Application()
objWord = oWord.Documents.Open(Application.StartupPath & "\DOCUMENT TRANSMITTAL FORM.doc")

Sometimes the word document it uses is already in open, and it pops up with a file in use box and then gives you three options
1. Open read only
2. Create another copy and merge later
3. be notified when the file becomes free

How would i automatically choose the option number 2. Create another copy and merge later

Adam Pedley
Question by:adam_pedley
  • 2
LVL 27

Expert Comment

ID: 12015374
Use this function will help, if you want to remove a running Word app.
Then you will not get the three options.

    Public Function WordInstance() As Boolean
        Dim sPros As Process
        For Each sPros In Diagnostics.Process.GetProcessesByName("WINWORD")
            If sPros.ProcessName = "WINWORD" Then
                Return True
                Return False
            End If
    End Function

Expert Comment

ID: 12015485
The problem I think he has is that somebody else has the word doc open.

The way I've handled this in the past is I used. DOCUMENT TRANSMITTAL FORM.doc as kind'a a template document and when the user opened it I saved it as something else right away or I just made it a word template.

Accepted Solution

wguerram earned 500 total points
ID: 12019715
This try to lock the file, if successfull then open the file for read write other wise readonly.

later the user can merge the files from Tools->Compare and Merge menu option

Hope this help.

Dim oWord As New Word.Application()
objWord = oWord.Documents.Open(Application.StartupPath & "\DOCUMENT TRANSMITTAL FORM.doc")

        If FileLocked(Application.StartupPath & "\DOCUMENT TRANSMITTAL FORM.doc") Then
            objWord = oWord.Documents.Open(Application.StartupPath & "\DOCUMENT TRANSMITTAL FORM.doc")
            objWord = oWord.Documents.Open(Application.StartupPath & "\DOCUMENT TRANSMITTAL FORM.doc", _ ReadOnly:=True, AddToRecentFiles:=False, Revert:=False)
        End If

Public Function FileLocked(ByVal strFileName As String) As Boolean
        Dim RetVal As Boolean
        'From Q209189: Microsoft
        'On Error Resume Next
        ' If the file is already opened by another process,
        ' and the specified type of access is not allowed,
        ' the Open operation fails and an error occurs.

        RetVal = True

        Dim fs As FileStream
            fs = File.Open(strFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
            RetVal = False
            fs = Nothing
        End Try

        Return RetVal
    End Function

Expert Comment

ID: 12019727
You have to import

Imports System.IO

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

17 Experts available now in Live!

Get 1:1 Help Now