Originally Posted by
atr140
The line Selection.HomeKey Unit:=wdStory is causing an error:
Run-time '438' Object does not support this property or method.
Withoiut looking to then merits of the code, but simply to address the error message - when using LateBinding to the Word object as in your code, you cannot use Word specific values such as wdStory. You must use instead the numeric equivalents of such commands. The enumeration value for wdStory is 6.
Paul's code example uses early binding to the Word object and so may be used with such values. There are advantages and disadvantages to both approaches. Programming is easier with early binding. Late binding makes the code more transportable - especially between different Office versions.
FWIW Paul's code could also be used with late binding
Sub OpenWordDoc(StrDocNm As String)
'Note: Late binding no reference to the Word library required
Dim wdApp As Object, wdDoc As Object, StrTxt As String, bStart As Boolean
If Dir(StrDocNm) = "" Then Exit Sub
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
bStart = True
End If
On Error GoTo 0
Set wdDoc = wdApp.Documents.Open(FileName:=StrDocNm, ReadOnly:=False, AddToRecentFiles:=False)
With wdDoc
'process the document
StrTxt = Replace(Replace(Replace(.Range.Text, Chr(11), "¶"), Chr(12), "¶"), vbCr, "¶")
'close
.Close SaveChanges:=False
End With
If bStart Then wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub