Consulting

Results 1 to 6 of 6

Thread: Open document as "Read ONLY" only

  1. #1
    VBAX Regular
    Joined
    Oct 2005
    Posts
    6
    Location

    Open document as "Read ONLY" only

    I am creating an excel spreadsheet that will list form field values from word documents. I am to the point that the basic flow is complete and am working on error handling.

    The Error that I would like to correct (not handle) is the case where the document that I am trying to access is already opened. The way the code is written, the infamous " document is in use, open as read only, notify or cancel" message box comes up. I want to automatically select read only. Is this possible? How would I do this?

    Thanks in advance, and if this is not clear, let me know and I will try to clarify.
    -Dan aka "Aqua"

    [VBA]
    Private Sub CommandButton1_Click()
    Dim fs, d, f, s
    Dim strDir As String
    ' gets filepath
    strDir = Application.ActiveWorkbook.Path
    x = 2
    ' creates file system object
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(strDir)
    Set fc = f.Files

    ' opens word application
    Set Wdapp = CreateObject("Word.Application")

    ' opens each word document in filepath
    For Each f1 In fc
    If Right(f1.Name, 3) = "doc" Then
    If Left(f1.Name, 2) <> "~$" Then
    Set wddoc = Wdapp.documents.Open(strDir & "\" & f1.shortName)
    ' gets data from word document and places in excel
    With Wdapp.ActiveDocument
    Me.Cells(x, 1).Value = .FormFields("Text1").Result
    Me.Cells(x, 2).Value = .FormFields("Text2").Result
    Me.Cells(x, 3).Value = .FormFields("Text3").Result
    Me.Cells(x, 4).Value = .FormFields("Text4").Result
    x = x + 1
    End With
    Set wddoc = Nothing
    End If
    End If
    Next
    Set fs = Nothing
    Set f = Nothing
    Set fc = Nothing
    Wdapp.Quit
    End Sub
    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Aqua
    I am creating an excel spreadsheet that will list form field values from word documents. I am to the point that the basic flow is complete and am working on error handling.

    The Error that I would like to correct (not handle) is the case where the document that I am trying to access is already opened. The way the code is written, the infamous " document is in use, open as read only, notify or cancel" message box comes up. I want to automatically select read only. Is this possible? How would I do this?

    Thanks in advance, and if this is not clear, let me know and I will try to clarify.
    -Dan aka "Aqua"

    [VBA]
    Private Sub CommandButton1_Click()
    Dim fs, d, f, s
    Dim strDir As String
    ' gets filepath
    strDir = Application.ActiveWorkbook.Path
    x = 2
    ' creates file system object
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(strDir)
    Set fc = f.Files

    ' opens word application
    Set Wdapp = CreateObject("Word.Application")

    ' opens each word document in filepath
    For Each f1 In fc
    If Right(f1.Name, 3) = "doc" Then
    If Left(f1.Name, 2) <> "~$" Then
    Set wddoc = Wdapp.documents.Open(strDir & "\" & f1.shortName)
    ' gets data from word document and places in excel
    With Wdapp.ActiveDocument
    Me.Cells(x, 1).Value = .FormFields("Text1").Result
    Me.Cells(x, 2).Value = .FormFields("Text2").Result
    Me.Cells(x, 3).Value = .FormFields("Text3").Result
    Me.Cells(x, 4).Value = .FormFields("Text4").Result
    x = x + 1
    End With
    Set wddoc = Nothing
    End If
    End If
    Next
    Set fs = Nothing
    Set f = Nothing
    Set fc = Nothing
    Wdapp.Quit
    End Sub
    [/VBA]
    Always open as read-only?

    [vba]
    Set wddoc = Wdapp.documents.Open(strDir & "\" & f1.shortName,readonly:=true)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by xld
    Always open as read-only?

    [vba]
    Set wddoc = Wdapp.documents.Open(strDir & "\" & f1.shortName,readonly:=true)
    [/vba]
    Same idea I had in mind...

  4. #4
    VBAX Regular
    Joined
    Oct 2005
    Posts
    6
    Location
    Well, I tried this option...and if the file is open, I still get the prompt to choose Read-Only, Notify or cancel. I am trying to surpress this prompt.

    A copy of the prompt is attached as well as my test files. IN order to recreate the prompt, just open one of the .docs before executing the code.

    Thanks.

    Dan

  5. #5
    VBAX Regular
    Joined
    Oct 2005
    Posts
    6
    Location
    Quote Originally Posted by xld
    Always open as read-only?

    [vba]
    Set wddoc = Wdapp.documents.Open(strDir & "\" & f1.shortName,readonly:=true)
    [/vba]
    XLD - Are you able to assist with this? I really need to find a way to surpress this prompt. Any direction would be appreciated. I posted the following, but since it was not a reply, I figured you may not have noticed the new post.

    Well, I tried this option...and if the file is open, I still get the prompt to choose Read-Only, Notify or cancel. I am trying to surpress this prompt.

    A copy of the prompt is attached as well as my test files. IN order to recreate the prompt, just open one of the .docs before executing the code.

    Thanks.

    Dan

  6. #6
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Not tested, but could you try adding

    [VBA]WdApp.DisplayAlerts=wdAlertsNone[/VBA]

    prior to opening the file and

    [VBA]WdApp.DisplayAlerts=wdAlertsAll[/VBA]

    after the file opens?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •