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
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