Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Opening word document

Posted on 2004-09-08
4
Medium Priority
?
2,140 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
[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
  • 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 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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