PDA

View Full Version : [SOLVED:] Check to see if word document is already open or read only



cjmitton
08-10-2018, 01:30 AM
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 (https://rtmccormick.com/2015/11/23/ms-word-document-text-to-string-with-vba/) 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?

gmaxey
08-10-2018, 05:51 AM
Changing:

oWord.Documents.Open(iFile)

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

seems to work.

cjmitton
08-10-2018, 06:19 AM
Great, just tried it and it worked.

Thank you Greg.

gmaxey
08-10-2018, 06:39 AM
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

cjmitton
08-10-2018, 07:29 AM
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.

gmaxey
08-10-2018, 08:50 AM
or to make that crystal clear:

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

cjmitton
08-13-2018, 03:59 AM
Great. thank you.