With - EndWith Loop problem re-sent

  • Thread starter Charles DiGiovanna
  • Start date
C

Charles DiGiovanna

Since I've not had any reply to my initial inquiry on this issue I've
decided to expand on it in hopes that someone can help.
The code that's failing is called by the line

FormatMsgText oMessage.ReceivedTime, oMessage.Body, oMessage.SenderName

In the procedure that selects the messages to be processed. (attached)
The code itself is:
*************************************
Public Sub FormatMsgText(RDate As Date, RBody As String, RSender As String)

' Declare for Word
Dim MyWordApp As Word.Application
Dim MyWordDocument As Word.Document
Dim MyStoryRange As Word.Range: Dim MyDateRange As Word.Range: Dim
MySenderRange As Word.Range
Dim R As Word.Range

' Create a new document for the message content
Set MyWordApp = New Word.Application
Set MyWordDocument = Documents.Add
Set MyStoryRange = MyWordDocument.StoryRanges(wdMainTextStory)

'Replace the plus signs in the text with spaces
MyStoryRange.Text = RDate & " " & RSender & RBody
On Error GoTo eHandler
With MyStoryRange.Find
..Text = "+"
..Replacement.Text = " "
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
MyStoryRange.Find.Execute Replace:=wdReplaceAll

' Print the reformatted message
ActiveDocument.PrintOut

' Clean up and close up
MyWordApp.Quit
Set MyWordDocument = Nothing
Set MyWordApp = Nothing
Exit Sub

' error handling code
eHandler:
MyWordApp.Quit
If Err = 5 Then
MsgBox ("Error 5")
Else
MsgBox ("Error Number = " + Err)
End If

End Sub
******************************
This seems to me to be perfectly straightforward code and the With...End
With loop works just fine if I use it in a procedure in a Word macro.
A Watch on the variable MyStoryRange shows that it contains the text that I
expect it to contain so that it's apparently ready to be handled by the With
loop. Yet, the procedure fails every time when called from the procedure in
the attachment. I get an Error 5 message.
Please help.
Thanks.
CVDG
 
S

Sue Mosher [MVP]

Maybe you need to be asking in a Word forum, since the issue you're having seems to be with Word, not Outlook, and explain which statement in the FormatMsgText procedure generates the error.

FWIW, instead of Word's Find, I would have used the simple Replace() function to replace + signs with spaces before setting the text, maybe something like this (untested):

strText = RDate & " " & RSender & RBody
strText = Replace(strText, "+", " ")
MyStoryRange.InsertAfter strText
MyWordDocument.PrintOut
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 

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