• Privacywetgeving
    Het is bij Helpmij.nl niet toegestaan om persoonsgegevens in een voorbeeld te plaatsen. Alle voorbeelden die persoonsgegevens bevatten zullen zonder opgaaf van reden verwijderd worden. In de vraag zal specifiek vermeld moeten worden dat het om fictieve namen gaat.

versturen als .pdf maar heeft steeds andere naam

Status
Niet open voor verdere reacties.

vrouw

Terugkerende gebruiker
Lid geworden
27 mrt 2010
Berichten
1.434
Ik gebruik onderstaande macro om een bestand per mail naar mijn mail te sturen als Exel format.
Ik wil het echter als een .pdf naar mijn mail sturen.
Het probleem is echter dat het bestand iedere keer een ander naam heeft waarmee het word opgeslagen.

Hoe krijg ik het voor elkaar om het in een mail te krijgen als pdf zoals het onderstaande macro ook met een excel format gaat.

Code:
Sub Send_Mail()

 Application.DisplayAlerts = False

'Working in 97-2007
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    
    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007
            'We exit the sub when your answer is NO in the security dialog that you only
            'see  when you copy a sheet from a xlsm file with macro's disabled.
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "Your answer is NO in the security dialog"
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With

   
    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    'TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
    ' TempFileName = " " & " " & Format(Now, "dd-mmm-yy h.mm uur")
     TempFileName = ActiveSheet.Name & " " & Format(Now, "dd-mmm-yy h.mm uur")
     
    With Destwb
      '  .Sheets(1).Shapes("CommandButton1").Delete
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        .Close savechanges:=False
    End With

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

        iConf.Load -1    ' CDO Source Defaults
        Set Flds = iConf.Fields
        With Flds
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.xxx.nl"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            .Update
        End With
                                     
    With iMsg
        Set .Configuration = iConf
        .To = "test@test.nl"
        .CC = ""
         .BCC = ""
        .FROM = "test@test.nl"
        .Subject = "" & ActiveSheet.Range("BB2").Text & ActiveSheet.Range("BB3").Text & " voor " & ActiveSheet.Range("BB4").Text & ""
        .TextBody = "Beste " & vbCrLf & vbCrLf & _
       "Hierbij " & ActiveSheet.Range("BB2").Text & ActiveSheet.Range("BB3").Text & " voor " & ActiveSheet.Range("BB4").Text & ""
      
           
        .AddAttachment TempFilePath & TempFileName & FileExtStr
        .Send
    End With
 
 'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    
        
     CreateObject("WScript.Shell").Popup " De Factuur is nu ook naar je eigen Mail gestuurd  !!", 2, " Ik sluit vanzelf na 2 Seconden"
End Sub
 
De code van Ron de Bruin is vergelijkbaar met die van de macrorecorder: verwijder eerst alle overbodige regels.
Door dat te doen ga je de code meteen vanzelf beter begrijpen.
 
Maar bij ron de Bruin staan alleen code`s voor outlook om te versturen als .pdf
 
Je bedoelt dit?
Code:
Sub Send_Mail()
    Dim iConf As Object
    Dim Flds As Variant
    Dim PDFnaam As String

    PDFnaam = ThisWorkbook.Path & "\Naam.pdf"
    ActiveWorkbook.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=PDFnaam, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1
    Set Flds = iConf.Fields
    With Flds
        .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") = "xxxxxx@gmail.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxx"
        .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
                                     
    With iMsg
        Set .Configuration = iConf
        .To = "ontvanger@prov.nl"
        .CC = ""
        .BCC = ""
        .FROM = "xxxxxx@gmail.com"
        .Subject = "Het onderwerp"
        .TextBody = "Aanhef"
        .AddAttachment PDFnaam
        .Send
    End With
End Sub
 
Ga het vanavond thuis proberen, thanks alvast.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan