• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2147
  • Last Modified:

Opening word document

Hi

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

thanks
Adam Pedley
0
adam_pedley
Asked:
adam_pedley
  • 2
1 Solution
 
planoczCommented:
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
                sPros.Kill()
                Return True
            Else
                Return False
            End If
        Next
    End Function
0
 
thenrichCommented:
Planoz,
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.
0
 
wguerramCommented:
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")
        Else
            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
        Try
            fs = File.Open(strFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
            fs.Close()
        Catch
            RetVal = False
        Finally
            fs = Nothing
        End Try

        Return RetVal
    End Function
0
 
wguerramCommented:
You have to import

Imports System.IO
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now