Solved

Opening word document

Posted on 2004-09-08
4
2,124 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You have to import

Imports System.IO
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

15 Experts available now in Live!

Get 1:1 Help Now