Range object in Word behaving strangely

B

Bob

Fellow VB/VBA coders,

I have the following VB code snippet which appends some text to the end of
the specified document and formats the text according the parameters
provided.

When I run the program, the code works fine in most instnaces, but sometimes
the range object seems to include text that was already in the document and
so I am getting incorrect formatting on the "extra" characters.

What can I do to ensure that the range object contains ONLY to characters I
wish to add?

Cheers

Bob



Sub AddTextToEndOfDocument(appWD As Word.Application, docWD As
Word.Document, strTextToAdd As String, bMakeBold As Boolean, bMakeItalic As
Boolean, bMakeUnderline As Boolean, iFontSize As Integer, strFontName As
String)
'
' This routine will add a block of text to the end of the current word
document
' The font and size of the text is set based upon the parameters
' Parameters
' Entry
' strTextToAdd Text string to insert into document
' bMakeBold Mark the text as bold
' bMakeItalic Mark the text as italic
' iFontSize Font size fo text
' strFontName Font Name for text
' Exit
' None
'
Dim rangeWD As Word.Range
Dim iNoOfCharacters As Long
'
' Set this document to be the active document
'
docWD.Activate
'
' Determine the content size
'
iNoOfCharacters = appWD.ActiveDocument.Characters.Count
'
' Set current range to end of documment
'
Set rangeWD = appWD.ActiveDocument.Range(Start:=0, End:=iNoOfCharacters)
rangeWD.MoveStart wdCharacter, iNoOfCharacters
'
' Add the text
'
rangeWD.Text = strTextToAdd
'
' Set range to added text
'
Set rangeWD = appWD.ActiveDocument.Range(Start:=iNoOfCharacters - 1,
End:=appWD.ActiveDocument.Characters.Count)
'
' Set the font size and name
'
rangeWD.Font.Size = iFontSize
rangeWD.Font.Name = strFontName
'
' Make bold if required
'
If bMakeBold Then
rangeWD.Font.Bold = True
Else
rangeWD.Font.Bold = False
End If
'
' Make italic if required
'
If bMakeItalic Then
rangeWD.Font.Italic = True
Else
rangeWD.Font.Italic = False
End If
'
' Make Underline if required
'
If bMakeUnderline Then
rangeWD.Font.Underline = True
Else
rangeWD.Font.Underline = False
End If
End Sub
 
C

Cindy M -WordMVP-

Hi Bob,
When I run the program, the code works fine in most instnaces, but sometimes
the range object seems to include text that was already in the document and
so I am getting incorrect formatting on the "extra" characters.

What can I do to ensure that the range object contains ONLY to characters I
wish to add?
There are some odd things in your code...

1)
If you set up an object variable for the document
docWD.Activate
then actually USE it, don't go playing around with "ActiveDocument" and such.
This
iNoOfCharacters = appWD.ActiveDocument.Characters.Count
should be this
iNoOfCharacters = docWD.Characters.Count
(This also means you probably don't need to .Activate docWD at all!)

2)
Word ranges include everything that might be in the text flow. Not just
"visible" text, but also hidden text and even fields codes. When you try to
use the character count to set a range, and you don't specify
TextRetrievalMode, the number of characters probably don't include such hidden
items. But when you specify a Range.Start and .End Word usually DOES include
them.

If all you really want is the end of the document, then do this:

Set rng = docWD.Range
rng.Collapse wdCollapseEnd

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
B

Bob

Thanks Cindy,

The code snippet is actually a sub-routine and the word.document is passed
in as a parameter, so it is already instanciated before the call to the
routine.
I'll tryout the techniques you have suggested

Cheers

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top