Solved

Opening word document

Posted on 2004-09-08
4
2,135 Views
Last Modified: 2008-03-03
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
Comment
Question by:adam_pedley
  • 2
4 Comments
 
LVL 27

Expert Comment

by:planocz
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
                sPros.Kill()
                Return True
            Else
                Return False
            End If
        Next
    End Function
0
 
LVL 5

Expert Comment

by:thenrich
ID: 12015485
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
 
LVL 8

Accepted Solution

by:
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")
        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
 
LVL 8

Expert Comment

by:wguerram
ID: 12019727
You have to import

Imports System.IO
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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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