atr140
02-14-2018, 03:30 PM
All,
I am trying to piece through a word document line by line, and write that info to a excel worksheet. I found some code to piece through a word doc however I am having some issue altering the code as I am unfamiliar with Word VBA.
See below for code:
I am sending a path to this sub to open the word doc. I have the oDoc dim outside the routine for use in other routines.
Sub OpenWordDoc(strPath As String)
'Open an existing Word Document from Excel
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
' Should open as the forefront
objWord.Activate
'Change the directory path and file name to the location
'of the document you want to open from Excel
Set oDoc = objWord.Documents.Open(strPath)
Call LoopingText
objWord.Quit
End Sub
I am then using the following code to loop through the word document and add each line to a string 'outputStr'. This sub is causing me issue. This sub was originally the routine written for Word.
Sub LoopingText() ''()
'Will move to the end of each line in the document and move the text to match
'Declare variables
Dim outputStr As String
Dim currLine As String
Dim endChar As String
Dim numOfLines As Integer
'Count the number of non blank lines in current document
numOfLines = oDoc.BuiltinDocumentProperties("NUMBER OF LINES")
oDoc.Activate
'Move to start of document
Selection.HomeKey Unit:=wdStory
'Start the loop - looping once for each line
For x1 = 1 To numOfLines
'Move to start of line
Selection.HomeKey Unit:=wdLine
'Select entire line and copy into variable currLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
currLine = Selection.Range.Text
'Remove final character (line break) from currLine
currLine = Left(currLine, (Len(currLine) - 1))
outputStr = outputStr & currLine
'Move down one line
Selection.MoveDown Unit:=wdLine, Count:=1
'Move to the next part of the loop
Next x1
End Sub
The line Selection.HomeKey Unit:=wdStory is causing an error:
Run-time '438' Object does not support this property or method.
I have tried:
oDoc.Selection.HomeKey Unit:=wdStory
oDoc.HomeKey Unit:=wdStory
Any help would be really appreciated. Thanks
I am trying to piece through a word document line by line, and write that info to a excel worksheet. I found some code to piece through a word doc however I am having some issue altering the code as I am unfamiliar with Word VBA.
See below for code:
I am sending a path to this sub to open the word doc. I have the oDoc dim outside the routine for use in other routines.
Sub OpenWordDoc(strPath As String)
'Open an existing Word Document from Excel
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
' Should open as the forefront
objWord.Activate
'Change the directory path and file name to the location
'of the document you want to open from Excel
Set oDoc = objWord.Documents.Open(strPath)
Call LoopingText
objWord.Quit
End Sub
I am then using the following code to loop through the word document and add each line to a string 'outputStr'. This sub is causing me issue. This sub was originally the routine written for Word.
Sub LoopingText() ''()
'Will move to the end of each line in the document and move the text to match
'Declare variables
Dim outputStr As String
Dim currLine As String
Dim endChar As String
Dim numOfLines As Integer
'Count the number of non blank lines in current document
numOfLines = oDoc.BuiltinDocumentProperties("NUMBER OF LINES")
oDoc.Activate
'Move to start of document
Selection.HomeKey Unit:=wdStory
'Start the loop - looping once for each line
For x1 = 1 To numOfLines
'Move to start of line
Selection.HomeKey Unit:=wdLine
'Select entire line and copy into variable currLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
currLine = Selection.Range.Text
'Remove final character (line break) from currLine
currLine = Left(currLine, (Len(currLine) - 1))
outputStr = outputStr & currLine
'Move down one line
Selection.MoveDown Unit:=wdLine, Count:=1
'Move to the next part of the loop
Next x1
End Sub
The line Selection.HomeKey Unit:=wdStory is causing an error:
Run-time '438' Object does not support this property or method.
I have tried:
oDoc.Selection.HomeKey Unit:=wdStory
oDoc.HomeKey Unit:=wdStory
Any help would be really appreciated. Thanks