Question

I am currently creating a large script to automate a Microsoft word document to pull out tables and put them into a new document. But I need to know when I reach the end of the document so I can move on to the next document.

Set objWord         = CreateObject("Word.Application")
Set objNewDoc       = objWord.Documents.Add()
Set objNewSelection = objWord.Selection
Set objDoc = objWord.Documents.Open( C:/Users/blahdoc.doc )
Set objSelection = objWord.Selection

This isn't the script but its how I defined and opened the documents for reading. I will happily insert more details if and when there needed.

I did look around for similar questions but didn't find any that apply. If you do sorry ahead of time ;)

Was it helpful?

Solution

You actually don't need to worry about "reaching the end of the document." Thankfully, the tables are stored in a Tables collection which is a property of a Word.Document. You can iterate through all the tables like so:

For Each oTable In objNewDoc.Tables
    If Left(oTable.Cell(1, 1).Range.Text, Len(oTable.Cell(1, 1).Range.Text) - 2) = "Some string" Then
        MsgBox "Found one!"
    End If
Next

One issue I ran into when putting this together is that all Cells' Text have an End-of-Cell Marker composed of two characters: a Carriage Return (ascii 13) followed by a BELL (ascii 7). I used Left to strip those off so I could compare the text against a string value, which is what I understand you are trying to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top