Fra Excel til PDF og sendes som vedhæftet - VBA
Har brug for lidt hjælp med mht til VBA kodning.Opgaven går ud på at udskrive en arkfane som pdf-udskrift og sendes som vedhæftet via MS Outlook (2003).
Selve VBA-koden på udskrivning til PDFD er lykkdes, men at vedhæftet pfd filen lykkedes ikke første gang. Men når koden køres igen, bliver pdf-filen vedhæftet. Problemet er at pdf-filen når ikke at blive generet færdig, inden der bliver sendt en mail.
Hvordan kan nedenstående kode tager hensyn til, således at mailen først skal vedhæfte pdf-filen, når den er blevet generet færdig.
-------------------------------------------------
Sub PrintSheetAsPDFwithBullZip()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileNames As String
Dim FileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
'must add a reference to BullZip
Dim myobject As New Bullzip.PDFPrinterSettings
Dim SavePath As String, FileName As String
SavePath = Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Desktop\"
FileNames = Range("A1") 'InputBox("Save PDF to desktop as:", "Sheet '" & ActiveSheet.Name & "' to PDF...", ActiveSheet.Name)
If LCase(Right(FileName, 4)) <> ".pdf" Then FileName = FileNames & ".pdf"
'see default settings in
'Environ("HOMEDRIVE") & Environ("APPDATA") & "\Bullzip\PDF Printer\settings.ini"
myobject.SetValue "output", SavePath & FileName
myobject.SetValue "showsettings", "never"
myobject.SetValue "ConfirmOverwrite", "no"
myobject.SetValue "showPDF", "no"
myobject.SetValue "ShowProgressFinished", "no"
myobject.WriteSettings (True) 'writes the settings in a runonce.ini that it immediately deleted after being used.
'change to bullzip printer...
If InStr(ActivePrinter, "PDF Printer på Ne00") = 0 Then
Dim storeprinter$, PrinterChanged As Boolean
PrinterChanged = True
OldPrinter = ActivePrinter
ActivePrinter = "PDF Printer på Ne00" 'GetFullNetworkPrinterName("BullZip")
End If
ActiveSheet.PrintOut
'If PrinterChanged Then ActivePrinter = storeprinter
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.From = Range("E5").Value
.To = Range("B1")
'.CC = Range("E4").Value & "; " & Range("E3").Value
.BCC = ""
.Subject = "Test"
.Body = ""
.Attachments.Add SavePath & FileName
'You can add other files also like this
.Display
'Send
End With
'On Error GoTo 0
'.Close saveChanges:=False
'End With
'Delete the file you have send
'Kill TempFilePath & TempFileName & FileExtStr
SendKeys "%{s}", True
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
'Application.ActivePrinter = strCurrentPrinter
End Sub
---------------------------------