F
Fetching text from MSWord protected area
I need to write a script to fetch text from an opened word document. I am
using the below script;
---------------------------------------------------------------------------------------------
Dim wrdApp
Dim wrdDoc
Dim tString, tRange
Dim p, startRange, endRange
Dim searchString
Set wrdApp = GetObject(,"Word.Application")
Set wrdDoc=wrdApp.ActiveDocument
MsgBox wrdApp.ActiveDocument.Name
tString = ""
With wrdDoc
For p = 1 To .Paragraphs.Count
startRange = .Paragraphs(p).Range.Start
endRange = .Paragraphs(p).Range.End
Set tRange = .Range(startRange, endRange)
tString = tString & tRange.Text
tString = Left(tString, Len(tString) - 1) 'exclude the paragraph-mark
Next
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
MsgBox tString 'captured text
---------------------------------------------------------------------------------------------
Unfortunately, I am encountering an error with few documents. The error
message is - 'This method or property is not available because the 'object
refers to a protected area of the document'. This is happening for the line -
'tString = tString & tRange.Text'
Your help is highly appreciated.
Joshy
847.667.7420
using the below script;
---------------------------------------------------------------------------------------------
Dim wrdApp
Dim wrdDoc
Dim tString, tRange
Dim p, startRange, endRange
Dim searchString
Set wrdApp = GetObject(,"Word.Application")
Set wrdDoc=wrdApp.ActiveDocument
MsgBox wrdApp.ActiveDocument.Name
tString = ""
With wrdDoc
For p = 1 To .Paragraphs.Count
startRange = .Paragraphs(p).Range.Start
endRange = .Paragraphs(p).Range.End
Set tRange = .Range(startRange, endRange)
tString = tString & tRange.Text
tString = Left(tString, Len(tString) - 1) 'exclude the paragraph-mark
Next
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
MsgBox tString 'captured text
---------------------------------------------------------------------------------------------
Unfortunately, I am encountering an error with few documents. The error
message is - 'This method or property is not available because the 'object
refers to a protected area of the document'. This is happening for the line -
'tString = tString & tRange.Text'
Your help is highly appreciated.
Joshy
847.667.7420