Results 1 to 2 of 2
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    New York
    Thanked 0 Times in 0 Posts

    Emailing ActiveDocument Via Macro (Word 2000)

    Does anyone have a code snippet that would illustrate how to send the current document via email.

    Preferrably not as an attachment -- either as plain text or as html embedded in Outlook.

    I'm working on a template that will scan the XP Personal Firewall log file, delete any attacks that don't come from a specific IP address, and then email it to the ISP. I'm doing it for Road Runner, but I assume it could be modified for any ISP if anyone's interested.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Silicon Valley, USA
    Thanked 93 Times in 89 Posts

    Re: Emailing ActiveDocument Via Macro (Word 2000)

    Here is something I wrote this afternoon and am still working on. It sweeps through a 2-column table which has labels (e.g., Payee: ) in the first column and FormFields in the second column. (The .StatusText of each control is a unique label.)

    This code creates an RTF message. I think this is a limitation of Outlook, that writing to the .Body creates RTF unless you write to the .HTMLBody, but I haven't dug into that yet. If RTF is not useful to you, you might have to do HTML, perhaps faking plain text with PRE tags.

    <pre>Sub MakeMessage()
    'Generate new message to Accounts Payable
    Dim olApp As Outlook.Application, olNS As Outlook.NameSpace
    Dim olNewMsg As Outlook.MailItem
    'Set object reference to Outlook 2000 and, if nec. start it/logon
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then 'Outlook was not open...
    On Error GoTo 0
    Set olApp = CreateObject("Outlook.Application")
    Set olNS = olApp.GetNamespace("MAPI")
    On Error GoTo 0
    Set olNS = olApp.GetNamespace("MAPI")
    End If
    'Create and populate new message
    Set olNewMsg = olApp.CreateItem(olMailItem)
    With olNewMsg
    Dim recip As Recipient
    Set recip = .Recipients.Add("")
    recip.Type = olTo
    Set recip = .Recipients.Add("")
    recip.Type = olCC
    .Subject = "Check Req/Needed: " & ActiveDocument.FormFields("ReqdDate").Result
    .Body = "Hello, Accounts Payable. This Check Request is being submitted by " & _
    "email only. Thanks for your help!" & vbCrLf & vbCrLf
    Dim intRows As Integer, intRowCounter As Integer
    Dim tbl As Table, strText As String, frmFld As FormField
    Set tbl = ActiveDocument.Tables(1)
    intRows = tbl.Rows.Count
    For intRowCounter = 2 To intRows
    If tbl.Cell(intRowCounter, 2).Range.FormFields.Count = 0 Then
    'this is meant to be copied over as text, but can't from protected doc
    .Body = .Body & "<Row " & intRowCounter & " blank>" & vbCrLf
    'tack on the non-empty form field values, separated by VbCrLf
    For Each frmFld In tbl.Cell(intRowCounter, 2).Range.FormFields
    If Len(frmFld.Result) > 0 Then
    .Body = .Body & frmFld.StatusText & " " & frmFld.Result & vbCrLf
    End If
    End If
    End With
    If Not frmFld Is Nothing Then Set frmFld = Nothing
    If Not tbl Is Nothing Then Set tbl = Nothing
    If Not recip Is Nothing Then Set recip = Nothing
    If Not olNewMsg Is Nothing Then Set olNewMsg = Nothing
    If Not olNS Is Nothing Then Set olNS = Nothing
    If Not olApp Is Nothing Then Set olApp = Nothing
    End Sub</pre>

    A long time ago I tried to manipulate the "envelope" that opens when you choose File|Send to|Mail Recipient but I could never crack how to do it. If it could be done, I believe it would be more mail client-independent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts