Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Object Model

Status
Not open for further replies.

segmentationfault

Programmer
Jun 21, 2001
160
US
I'm pretty well versed in interacting with Excel through VBA, as some may know, however I haven't touched Word with VBA until today.

I am having a terrible time figuring out how to read and process the text. For example, I want to write a macro that will turn VBA comments (' as the first non-whitespace character on the line) green in a Word document. Can I read the document line by line?

What can be done with the Paragraph object? Is it possible to access the characters on the page in a line and column fashion similar to row and column in Excel?
 
I'm no VBA Word expert but I took this from one of my loops.....its looking for each instance of a string "Figure #?:" (using ? us a wildcard) then it returns the dynamic pagenumer of the document it's on and total number of pages.
This is just a section of a much larger loop so it's missing End If's and End With's....


appWord.Selection.HomeKey Unit:=wdStory, Extend:=wdMove
Set myrange = ActiveDocument.Content

nextfigfind:

With myrange.Find
.ClearFormatting
.Forward = True
.Wrap = wdWdFindStop
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.Execute FindText:="Figure #?:"
If .Found = True Then
myrange.Select
myfig = myrange.Text
curpage = myrange.Information(wdActiveEndPageNumber)
totpage = ActiveDocument.Content.ComputeStatistics(wdStatisticPages)
End If
appWord.Selection.Find.ClearFormatting

'more code with the End With and End If's in there....
Thank you,
Dave Rattigan
 
Hi segmentationfault,

It is possible to read the document line by line. Use Input# function. I started thread in this forum about this and someone gave some very good examples of it. I was wondering if you are planning change the font green on a particular line of code. I am working a similar type of project except in the backward I am looking text that is red highlighted in font. I started by converting the word document into .rtf format and did a search for /strike for strikethrough and /cf6 which means red font. It might help to do a similar search for the string in this way and then insert /cf11 which means green font into front of the text.
Below is code how I am trying insert my text into the document. I have one glitch with it.

Private Sub InsertSymbol(RedStrikeText)
DocActivate

**** Here I am doing the search for the text

With ActiveDocument.Content.Find
.Text = RedStrikeText
.Execute

***** The glitch is this comparison below say it can't find my text in the document. I am still working on this.


If .Found = True Then
With .Replacement
.ClearFormatting
Dim NewRedStrikeText As String


***** Here I am try insert [& &] around the text

NewRedStrikeText = "[&" & RedStrikeText & "&]"
.Text = NewRedStrikeText
End With
.Execute Replace:=wdReplaceAll
End If
End With

End Sub



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top