Extra paragraph at the end of a VBA-generated EmailSignature

A

Adri Genis

Hi,

I was given the task to change a VBScript that uses MSWord automation to
create a Outlook Signature. I have the problem that it creates an empty
paragraph at the end. I have tried the following code to delete the
paragraph before saving the document, but at that stage the empty paragraph
doesn't exist yet. Any advice?

Set objSelection = objDoc.Paragraphs.Last.Range
If objSelection.Text = vbCr Then objSelection.Delete

Thanks,
Adri Genis
 
P

Pesach Shelnitz

Hi Adri,

When you terminate the last paragraph of a document with a carriage return,
Word creates an empty paragraph after it. You need to remove the carriage
return from the end of the last non-empty paragraph to prevent this from
happening. The following code does this.

Set objSelection = objDoc.Paragraphs.Last.Range
If objSelection.Characters.Last = vbCr Then
objSelection.start = objSelection.End - 1
objSelection.Delete
End If

You might not need to remove anything if your script would create the
signature without a terminating carriage return. If you have any further
question about this, please provide all the code that adds text to the Word
document.
 
A

Adri Genis

Hi,

The last part of my code follows:

======================================================================================

' CONTACT DETAIL
###############################################################
objSelection.Font.Size = 8
If strCell <> "" Then
strDisplayCell = " | Cell: " + strCell
End if
objSelection.TypeText "t " + strOffice + " | f " + strFax + " | c " +
strCell + " | "
objSelection.Hyperlinks.Add objSelection.range, "mailto:" & strEmail, ,
, strEmail
objSelection.TypeText " | "
objSelection.Hyperlinks.Add objSelection.range, "http://" & strweb, ,
strweb
' FORMAT HYPERLINKS
############################################################
For Each hLink In objDoc.Hyperlinks
hLink.range.font.Name = "century gothic"
hLink.range.font.color = wdColorBlue
hLink.range.font.bold = true
hLink.range.font.Size = 7
Next ' hLink
' WRITE MyDefaultSig
###########################################################
' ------------------------------------------------------------------------------
Set objSelection = objDoc.Range()
' ------------------------------------------------------------------------------
'
##############################################################################
objSignatureEntries.Add "MyDefaultSig", objSelection
objSignatureObject.NewMessageSignature = "MyDefaultSig"
' DELETE extra paragraphs
######################################################
Set objSelection = objDoc.Paragraphs.Last.Range
msgbox objSelection.Text
If objSelection.Text = vbCr Then objSelection.Delete
'
##############################################################################
objDoc.Saved = True
objWord.Quit

======================================================================================

the debug msgbox indicates that the last paragraph is the contact
information (email, web etc ...)

I assume my code (see DELETE extra paragraphs) is functionally the same as
the suggested code.

Regards,
Adri
 
P

Pesach Shelnitz

Hi Adri,

This code should not create an empty paragraph at the end, but since I don't
have your complete
script, I can't repro what you are seeing.

However, if I understand your code correctly, you are first setting your
objSelection object to the
Word Selection object, and then you reset it to a Range object. This may
work in VBScript, but
I suggest that you use different objects for Selection and Range. You can
never be certain that
something like this isn't causing the problem.
 

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