Consulting

Results 1 to 7 of 7

Thread: Check to see if word document is already open or read only

  1. #1
    VBAX Regular
    Joined
    Sep 2012
    Posts
    63
    Location

    Check to see if word document is already open or read only

    In a previous post about opening a word document and putting it in to a text box to 'preview' the content I came across a function on Ryan McCormick website I've started to tweak this for my own requirements.

    Function getPreviewWordDocText(iFile) As String
    
    
        Dim oWord As Object
        Dim oWdoc As Object
        Dim docContent As String
        Dim numChar As Long
        ' Initialize Word Objects
    On Error GoTo Errhandler
        Set oWord = CreateObject("Word.Application")
        Set oWdoc = oWord.Documents.Open(iFile)
        ' Count number of characters in document
            numChar = oWdoc.Characters.Count
        ' If greater than 750 then select first 750 to preview.
            If numChar >= 750 Then
                docContent = oWdoc.Range(0, 750)
            Else
        ' If less than 750 select all characters to preview
                docContent = oWdoc.Content
            End If
        ' Return Document Content
            getPreviewWordDocText = docContent
        ' Completed go to clean up
        GoTo ExitMacro
    
    
    Errhandler:
        Select Case Err
            Case 4608:
                ErrMsg = "There is an error with the file you are previewing" & _
                   "Press OK to continue"
                Result = MsgBox(ErrMsg, vbOKOnly)
                GoTo ExitMacro
             Case Else:
                MsgBox "Error in preview macro # " & Err & " : " & Error(Err)
                GoTo ExitMacro
            End Select
    ExitMacro:
        ' Clear Memory
        oWdoc.Close
        oWord.Quit
        Set oWdoc = Nothing
        Set oWord = Nothing
    End Function
    I've added in a character count a if there's less than the selected number of characters it crashes (hence the added error handler) so added the if statement to sort that issue.

    My main issue is now sometimes when opening the word document to count / read the text it waits in the background for a open read only / close / etc box if the word document is either open already or has not closed down correctly and someone still has it 'open'.

    I've tried to play with opening it read only (as that all I need anyway) but not had any luck, should I be checking first to see if its open or can I just force open read only? Or should I be doing something completely different?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,866
    Location
    Changing:

    oWord.Documents.Open(iFile)

    To
    oWord.Documents.Open(iFile, , True)

    seems to work.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Sep 2012
    Posts
    63
    Location
    Great, just tried it and it worked.

    Thank you Greg.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,866
    Location
    I would change the code to:

    Sub Test()
      MsgBox fcnGetPreviewWordDocText("D:\Test.docm")
    End Sub
    Function fcnGetPreviewWordDocText(strFileName) As String
    Dim oWord As Object
    Dim oDoc As Object
    Dim bIsRunning As Boolean
      bIsRunning = False
      On Error Resume Next
      Set oWord = GetObject(, "Word.Application")
      If Err.Number <> 0 Then
        Set oWord = CreateObject("Word.Application")
      Else
        bIsRunning = True
      End If
      Set oDoc = oWord.Documents.Open(strFileName, , True)
      If oDoc.Characters.Count > 750 Then
        fcnGetPreviewWordDocText = oDoc.Range(0, 750)
      Else
        fcnGetPreviewWordDocText = oDoc.Content
      End If
    lbl_Exit:
      oDoc.Close
      If Not bIsRunning Then oWord.Quit
      Exit Function
    End Function
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Sep 2012
    Posts
    63
    Location
    Excellent... was just starting to look at opening the document in the same instance of word to speed it up. That has speeded up the macro no end. The screen did flicker around even with various Application.ScreenUpdating = False' in the code so had a look at the Documents.Open method and changed it to:
    Set oDoc = oWord.Documents.Open(strFileName, , True, False, , , , , , , , False)
    That has then sorted out that. Thank you so much Greg.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,866
    Location
    or to make that crystal clear:

    .Open(FileName:=strFileName, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Sep 2012
    Posts
    63
    Location
    Great. thank you.

Posting Permissions

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