Share via

Emial current document as a PDF using CDO from ms word with attachment using gmail

Anonymous
2017-03-30T13:05:20+00:00

Hi i am trying below code to send my current document as a pdf with attachment but some how as its giving error message at

".Attachments.Add FileName & ".pdf" " runtime error '13' type mismatch

without " FileName & ".pdf" " its working fine but as i want to attach the exported file to send through email i am getting the above error.....  can any one please correct the code so that it can work for me.... code is below....

Sub SendEmail()

Dim objCDOConfig As Object

Dim objCDOMessage As Object

Dim CurrentFolder As String

Dim FileName As String

Dim myPath As String

Dim UniqueName As Boolean

UniqueName = False

'Store Information About Word File

  myPath = ActiveDocument.FullName

  CurrentFolder = ActiveDocument.Path & ""

  FileName = ActiveDocument.Bookmarks("scno").Range.Text

   'Save As PDF Document

      ActiveDocument.ExportAsFixedFormat _

     OutputFileName:=CurrentFolder & FileName & ".pdf", _

     ExportFormat:=wdExportFormatPDF

'setting smtp gmail mail

Const cdoschema = "http://schemas.microsoft.com/cdo/configuration/"

Const cdoSendUsingPort = 2

Const cdoBasic = 1

Const cstrServer = "smtp.gmail.com"

Const cstrFrom = "***********@gmail.com"

Set objCDOConfig = CreateObject("CDO.Configuration")

With objCDOConfig.Fields

.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "***********@gmail.com"

        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "*************"

        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465

        .Update

End With

'setting smtp gmail complete

Set objCDOMessage = CreateObject("CDO.Message")

With objCDOMessage

Set .Configuration = objCDOConfig

.From = "KL Prepaid Time Ext."

.Sender = cstrFrom

.To = "***********@gmail.com"

.Subject = "TEMP PREPAID TIME EXT. " & ActiveDocument.Bookmarks("scno").Range.Text

.TextBody = "Prepaid Time Extension"

.Attachments.Add FileName & ".pdf"

.Send

End With

Kill FileName & "*.pdf"

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2017-04-01T23:21:44+00:00

    Doing a Google search and looking at some of the responses, it appears that instead of

    .Attachments.Add CurrentFolder & FileName & ".pdf"

    you should be using

    .AddAttachment CurrentFolder & FileName & ".pdf"

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-04-01T06:50:24+00:00

    Hi ! Thanks for ur reply. attached is the error msg after adding the given code line...

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2017-03-31T09:42:54+00:00

    If you insert

    MsgBox CurrentFolder & FileName & ".pdf"

    What does it display when you run the code (you may need to comment out the .Attachments.Add.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-03-31T08:51:37+00:00

    Hi ! Thanks for your reply.

    Sorry to say but code 

    .Attachments.Add CurrentFolder & FileName & ".pdf"

    is also not working and giving the same error message. Kindly provide some other code that can work.

    Best Regards

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2017-03-30T23:00:20+00:00

    Try:

    .Attachments.Add CurrentFolder & FileName & ".pdf"

    Was this answer helpful?

    0 comments No comments