Macro om word bijlages als pdf door te sturen

Status
Niet open voor verdere reacties.

Lenny1991

Nieuwe gebruiker
Lid geworden
7 mei 2020
Berichten
3
Goedemiddag,

Ik heb een macro in outlook gemaakt waarmee ik bijlagen van e-mails kan opslaan, de onderwerpregel aanpas en de e-mail inclusief bijlage doorstuur.

Ik wil echter alleen pdf bijlages doorsturen en alle bijlages die niet in pdf vorm zijn eerst omzetten in pdf om ze daarna door te sturen. Dit zijn meestal bijlagen in .doc of .docx formaat.

Is het mogelijk bijlagen in .doc en .docx formaat te herkennen, deze om te zetten naar pdf en met mijn bestaande macro deze e-mails door te sturen?

Groet,
Lenny1991
 
Dat kan wel, als je in je macro een Word sessie opstart, daarin het document opent, vervolgens opslaat als PDF en vervolgens dan weer doorstuurt. Post de macro er eens bij, zou ik zeggen :).
 
Dankjewel voor je enthousiaste reactie!

Ik heb het grootste gedeelte van de macro uit openbare bronnen gehaald, aan elkaar geplakt en zelf enkele zaken gewijzigd. Ik ben me ervan bewust dat bepaalde delen van de macro op meer dan 1 e-mail tegelijkertijd toepasbaar zouden zijn en andere delen niet.

Het einddoel zou wel zijn om meerdere e-mails tegelijkertijd af te handelen, maar voor nu neem ik genoegen met 1 e-mail tegelijkertijd.

Het enige dat voor de doorgestuurde e-mails belangrijk is, is dat het onderwerp van de e-mails begint met [ink] en dat de bijlage dus een pdf is.

Hieronder de volledige macro. Uiteraard wordt naast de eerder gevraagde input ook op prijs gesteld als er verbeteringen aan de huidige code mogelijk zijn.

Code:
Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strcopiedfiles As String

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")

' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

' Set the Attachment folder.
strFolderpath = "C:\voorbeeld"

' Check each selected item for attachments. If attachments exist,
' save them to the strFolderPath folder and strip them from the item.
For Each objMsg In objSelection

    ' This code only strips attachments from mail items.
    ' If objMsg.class=olMail Then
    ' Get the Attachments collection of the item.
    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
    strcopiedfiles = ""

    If lngCount > 0 Then

        ' We need to use a count down loop for removing items
        ' from a collection. Otherwise, the loop counter gets
        ' confused and only every other item is removed.

        For i = lngCount To 1 Step -1

            ' Save attachment before deleting from item.
            ' Get the file name.
            strFile = objAttachments.Item(i).FileName

            ' Combine with the path to the Temp folder.
            strFile = strFolderpath & strFile

            ' Save the attachment as a file.
            objAttachments.Item(i).SaveAsFile strFile

            'write the save as path to a string to add to the message
            'check for html and use html tags in link
            If objMsg.BodyFormat <> olFormatHTML Then
                strcopiedfiles = strcopiedfiles & vbCrLf & "<file://" & strFile & ">"
            Else
                strcopiedfiles = strcopiedfiles & "<br>" & "<a href='file://" & _
                strFile & "'>" & strFile & "</a>"
            End If
        Next i

        ' Adds the filename string to the message body and save it
        ' Check for HTML body
        If objMsg.BodyFormat <> olFormatHTML Then
            objMsg.Body = vbCrLf & "Het bestand/de betanden zijn opgeslagen op " & strcopiedfiles & " en de factuur is verstuurd naar xxx." & vbCrLf & objMsg.Body
        Else
            objMsg.HTMLBody = "<p>" & "Het bestand/de betanden zijn opgeslagen op " & strcopiedfiles & " en de factuur is verstuurd naar xxx."& "</p>" & objMsg.HTMLBody 
        End If
        objMsg.Save
    End If
Next

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing

Dim email1 As String
Dim objMail As Outlook.MailItem
Dim strbody As String
Dim oldmsg As String
Dim senderaddress As String
Dim addresstype As Integer
Dim strsubject As String

email1 = "voorbeeld@voorbeeld.com"

Set objItem = GetCurrentItem()
objItem.UnRead = False
Set objMail = objItem.Forward

strbody = Replace(objItem.Body, objItem.Body, "")
objMail.To = email1
strsubject = "[ink]" & objItem.Subject
objMail.Subject = strsubject

objMail.Send

Set objItem = Nothing
Set objMail = Nothing

Dim myDestFolder As Folder
Dim ObjItem2 As Outlook.MailItem
Dim strMailboxName As String

strMailboxName = "voorbeeld1"
Set myDestFolder = Session.Folders(strMailboxName)
Set myDestFolder = myDestFolder.Folders("voorbeeld2")
Set myDestFolder = myDestFolder.Folders("Voorbeeld3")
Set ObjItem2 = GetCurrentItem()

ObjItem2.Move myDestFolder

End Sub

Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = _
objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = _
objApp.ActiveInspector.CurrentItem
Case Else
End Select
End Function
 
Laatst bewerkt:
Oef, dat is een hoop code :). Kun je die alsnog even opmaken met de CODE Tag (knop #). Dan is hij tenminste ook leesbaar :).
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan