• Send Email as Attachment via VBA

    Author
    Topic
    #460294

    I have been using the “Outlook Automation” code with a Word document (which has form fields filled in by the user), to send the document as an attachment (see code below). I added the save command at the beginning of the code. I have a checkbox on the form, which runs the code when it is checked.

    Since we switched to Office 2007, sometimes the document sends blank and sometimes it sends with the data filled in. I see the save command run so I know it saves. We have been opening the document via a link in IE.

    I saved a copy to a network folder and tried executing the code and get the error: “Run-time error 446 Object doesn’t support named arguments” on the .attachments line of code.

    Do I need to do something different for Office 2007? I can’t find any differences in the users that work and those that don’t.

    Any Ideas? Thanks!
    Patty

    You can send the document as attachment using Outlook provided the document has been saved at least once:

    Sub SendDocumentAsAttachment()

    Dim bStarted As Boolean
    Dim oOutlookApp As Outlook.Application
    Dim oItem As Outlook.MailItem

    On Error Resume Next

    ChangeFileOpenDirectory _
    “filepath”
    ActiveDocument.SaveAs FileName:= _
    “filename”, FileFormat:= _
    wdFormatDocument, LockComments:=False, Password:=””, AddToRecentFiles:= _
    True, WritePassword:=””, ReadOnlyRecommended:=True, EmbedTrueTypeFonts:= _
    False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    SaveAsAOCELetter:=False

    If Len(ActiveDocument.Path) = 0 Then
    MsgBox “Document needs to be saved first”
    Exit Sub
    End If

    Set oOutlookApp = GetObject(, “Outlook.Application”)
    If Err 0 Then
    Set oOutlookApp = CreateObject(“Outlook.Application”)
    bStarted = True
    End If

    Set oItem = oOutlookApp.CreateItem(olMailItem)

    With oItem
    .To = “recipient@mail.com”
    .Subject = “New subject”
    ‘Add the document as an attachment, you can use the .displayname property
    ‘to set the description that’s used in the message
    .Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue, _
    DisplayName:=”Document as attachment”
    .Send
    End With

    If bStarted Then
    oOutlookApp.Quit
    End If

    Set oItem = Nothing
    Set oOutlookApp = Nothing

    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #1163474

      Update – I tried saving it to a network folder and running the code again and it worked correctly.

      I noticed before and forgot to mention:
      When I send the form via the email code and it comes in blank
      then I look at the file in the directory indicated in the code
      the file contains the data from the last correct save and not from the save I just saw run with the code in the document

      So maybe it is a problem with the save command – but I don’t get an error on the save.

      Patty

      • #1163506

        Is the switch to Office 2007 the only change, or did your users also move to another Windows version, e.g. from Windows XP to Windows Vista?

    Viewing 0 reply threads
    Reply To: Send Email as Attachment via VBA

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: