MailEnvelope (MsoEnvelope)

J

Joop

Hi all

In an application I develope I want to send a word document via Outlook. So
I want to use MsoEnvelope to do that. I use the folowing code to accomplishe
that:

Private Sub CmdVerzEMail_Click()
Dim cn As ADODB.Connection
Dim rcdCor As New ADODB.Recordset
Dim SQLString As String
Dim DocPadString As String
Dim WApp As Object, OApp As Object
Set OApp = StartOutlook
Set cn = CurrentProject.Connection
SQLString = "SELECT * FROM TblCorres WHERE FldCorresID = " &
Me!TxtCorresID & ";"
rcdCor.Open SQLString, cn, adOpenKeyset, adLockOptimistic
DocPadString = rcdCor!FldCorresDir & rcdCor!FldCorresNaam & ".DOC"
Set WApp = StartWord
WApp.Documents.Open filename:=DocPadString
With WApp.ActiveDocument.MailEnvelope.Item
.To = "(e-mail address removed)"
.Subject = rcdCor!FldCorresOnderw
.send
End With
OApp.Application.Quit
WApp.Application.Quit wdDoNotSaveChanges
End Sub

StartOutlook and StartWord are functions that check if the application
already runs and if not it starts the application.

My question: Often when I run the code the Word document is not ready so the
"WApp.ActiveDocument.MailEnvelope.Item" generates an error (Object variable
or with block var not set). I think it is an timing problem. When I run the
code in debug mode it executes ok. How can I check if the objectproperties I
want to change are available??

thxx
Joop Aarts
 
B

Ben

you could try inserting a loop which only exits after the object is ready...

something like ..

--------------------------------------------------------------------------------


WHILE isValidObject(WApp.ActiveDocument.MailEnvelope.Item)
DoEvents
WEND

With WApp.ActiveDocument.MailEnvelope.Item
.To = "(e-mail address removed)"
.Subject = rcdCor!FldCorresOnderw etc...


--------------------------------------------------------------------------------

where isValidObject could be a function something like ....

--------------------------------------------------------------------------------

Function isValidObject(inObj As Object)
On Error GoTo InvalidObject
validobject = Len(inObj.Name) > 0
InvalidObject:
isValidObject = IIf(validobject, True, False)
End Function
 
B

Ben

Sorry I missed a crytical NOT out there..
(amendment below)

------------------------------------------------------------------------------


WHILE NOT(isValidObject(WApp.ActiveDocument.MailEnvelope.Item))
DoEvents
WEND
 

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

Similar Threads

MailEnvelope(MsoEnvelope) 1

Top