Hi Daniweb Community,
I have a application that opens a pdf viewer and I would like to email the current(one in the pdf viewer) as an attachment in Outlook. I have listed the code below that opens the pdf viewer and I have listed the code to send an email with Outlook. The problem is when I code to add an Attachment I cannot see where the viewer will let me add the attachment as the current or active document. Can someone help me on this?

' Here is the code that loads the document

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Form5 = Nothing
        Form5.Show()
        Dim filenameX As String
        filenameX = "P:\gdace\MSDS\Yellow Armour Aqua Enamel Sheboygan 1 10.pdf"
        Form5.AxAcroPDF1.LoadFile(filenameX)
    End Sub

Here is the code to send the email I just don't know how to attach the current document.


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oOApp As Microsoft.Office.Interop.Outlook.Application
        Dim oOMail As Microsoft.Office.Interop.Outlook.MailItem
        Dim oAddSig As Microsoft.Office.Interop.Outlook.Inspector
        'Create the Application
        oOApp = CreateObject("Outlook.Application")
        oOMail = oOApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
        oAddSig = oOMail.GetInspector
        'Send the E-Mail to the Recipient
        With oOMail
            .To = "mbrown"
            .CC = ""
            .Subject = "MSDS Sheet E-Mail Test"
            .Body = "Current MSDS Sheet"
            .Attachments.Add()This is where I am getting stuck
            .Send()
        End With
        Try
        Catch ex As Exception
            MsgBox("Email Sent to the Recipient")
        End Try
        
    End Sub

Dani AI

Generated

Short answer: the viewer itself doesn’t give you a convenient “current file path” to pull back later, so record the filename when the PDF is opened and attach that file (or a temp copy) when sending. Also make sure the same Form5 instance that opened the PDF is the one the send code reads from.

Example: add a small public property on Form5 and set it when loading the PDF.

' on Form5
Public Property ActivePdfPath As String

' when opening the PDF (set this at the same time you call LoadFile)
Dim pdfFile = "P:\gdace\MSDS\Yellow Armour Aqua Enamel Sheboygan 1 10.pdf"
Me.AxAcroPDF1.LoadFile(pdfFile)
Me.ActivePdfPath = pdfFile

Send routine (outline): verify the path, copy to a temp file to avoid locks, attach the temp file and send, then delete the temp file.

Dim path = If(Form5 IsNot Nothing, Form5.ActivePdfPath, "")
If String.IsNullOrEmpty(path) OrElse Not File.Exists(path) Then Exit Sub

Dim tmp = Path.Combine(Path.GetTempPath(), Path.GetFileName(path))
File.Copy(path, tmp, True)   ' defensive copy if Acrobat locks original

Dim ol = New Microsoft.Office.Interop.Outlook.Application()
Dim mail = CType(ol.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem), Microsoft.Office.Interop.Outlook.MailItem)
mail.To = "mbrown"
mail.Subject = "MSDS Sheet E-Mail Test"
mail.Body = "Current MSDS Sheet"
mail.Attachments.Add(tmp)
mail.Send()

Try
    File.Delete(tmp)
Catch : End Try

Notes and troubleshooting: ’s idea to use System.Net.Mail is valid (small typo in the posted snippet: strPatstrPath). If the PDF is generated in memory, attach a MemoryStream instead of writing disk files. Outlook automation can trigger security prompts; calling Display() before Send() or using SMTP (System.Net.Mail/SmtpClient) are common workarounds. Ensure the Form5 instance used to set ActivePdfPath is the same instance read by the sender.

Dim strPath As String
        strPath = "Path to PDF File"
        Dim attMent As New System.Net.Mail.Attachment(strPat)



mail.Attachments.Add(attMent)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.