WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Word

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Read Word Doc Line by Line


Question:

I am completely new to OLE, although I can write vanilla WBT and read word macros.

I wish to do something very straight forward:

  1. open a named word doc
  2. start from top, read each line for processing in wbt
  3. close doc.
If it were a text file, all I needed was fileOpen, fileRead fileClose, and to detect *EOF*. How do I do the equivalent task in a Word document?

Here is what I've got from a recorded macro. It is far from workable:

wordapp = objectopen("Word.Application")
docs=wordapp.documents
docs.open(docPathName)
wordapp.visible=@true
thisdoc = wordapp.activedocument
s=wordapp.selection
s.GoTo(wdGoToLine,1)

while @true
s.EndKey(:: Unit:=wdLine, Extend:=wdExtend)
s.Copy
line = clipGet()
; process line
s.MoveRight(:: Unit:=wdCharacter, Count:=1)
endWhile

thisdoc.close()
objectclose(thisdoc)
objectclose(docs)
objectclose(wordapp)

Answer:

Your code looks pretty good. I made a few modifications. Note: need to define the values of those Word constants.


;Constant values defined
wdGoToLine = 3
wdLine = 5
wdExtend = 1
wdCharacter = 1

file=  "%dir%\name.doc"

if appExist("winword.exe")
  oWORD  = ObjectAccess("Word.Application", @false)
  newWord = @false
else
  oWORD  = ObjectOpen("Word.Application")
  newWord = @true
endIf
oDOCS = oWORD.Documents
oDOC  = oDOCS.Open(file)

oWORD.visible=@true
oActiveDoc = oWORD.activedocument
oSel=oWORD.selection
oSel.GoTo(wdGoToLine,1)

count = 1
while @true
  oSel.EndKey(:: Unit=wdLine, Extend=wdExtend)
  oSel.Copy
  line = clipGet()
  Message("Line %count%",line)
  oSel.MoveRight(:: Unit=wdCharacter, Count=1)
  if oSel.MoveRight == 0 then break 
  oSel.MoveLeft(:: Unit=wdCharacter, Count=1)
  ; compensate for MoveRight
  count = count+1
endWhile

oActiveDoc.close()
objectclose(oSel)
objectclose(oActiveDoc)
objectclose(oDOC)
objectclose(oDOCS)
if newWord
  oWord.Quit
endIf
objectclose(oWORD)
exit


Article ID:   W16147
File Created: 2006:01:13:14:36:12
Last Updated: 2006:01:13:14:36:12