Run a macro from Code

J

Jeff C

I am cross posting here from the Access Group. I am exporting a report to an
RTF file and opening it in WORD from access with this:

Set wdApp = New Word.Application
wdApp.Visible = True

Set doc = wdApp.Documents.Open("T:\Reports\Report.rtf")
wdApp.Activate

I built a macro that will send the contents of this to the message body of
an email and send via outlook which works manually running the macro, and
works programatically from Access with one exception.

Without the wdApp.Activate the Word document does not have focus so the
macro code cant's find a document. With the wdApp.Activate it appears that
with Access losing focus to Word, the code quits running right after that
line.

How can I either run the Macro with the Word document or the Code on the
Word document?

I would really appreciate the help. Thank you
 
D

Doug Robbins - Word MVP

Show us the code. Otherwise, we are just guessing in the dark.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jeff C

I finally got the code corrected, the main problem I think was the line:
wdApp.Run

But to share, the following is a click event in an access database which
exports a report in RTF format and send the formatted output as the message
body of an email using the Macro whose code is at the bottom, which needs to
be built into your WORD application. I have been told by the Blackberry
users that attachments are difficult to read compared with the report text
formatted in the message body.

Private Sub Command5_Click()
DoCmd.OutputTo acOutputReport, "NameOfAccessReport", acFormatRTF,
"FullNameAndPathOfOutputFile"

Dim wdApp As Word.Application
Dim doc As Word.Document

Set wdApp = New Word.Application
Set doc = wdApp.Documents.Open("FullNameAndPathOfOutputFile")
wdApp.Run "NameOfMacro"
wdApp.Quit False
Set doc = Nothing
Set wdApp = Nothing

Kill ("FullNameAndPathOfOutputFile")

End Sub

Sub NameOfMacro()

Set Report = ActiveDocument.MailEnvelope.Item
Report.To = "RecipientEmailAddress"
Report.Subject = "WhatEverYouWant"
Report.Send
End Sub


Thanks Doug
--
Jeff C
Live Well .. Be Happy In All You Do


Doug Robbins - Word MVP said:
Show us the code. Otherwise, we are just guessing in the dark.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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