Sub PDF_Schiedam()
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 OutApp As Object
Dim sh As Worksheet
Userform2.Show 0
DoEvents
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Sheets("Blad 1").Select
ActiveSheet.Unprotect
Set Sourcewb = ActiveWorkbook
'Copy the sheets to a new workbook
Sourcewb.Sheets(Array("Blad 1")).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
For Each sh In Destwb.Worksheets
sh.Select
With sh.UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
Destwb.Worksheets(1).Select
Next sh
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
'Save the new workbook/Mail it/Delete it
'TempFilePath = Environ$("temp") & "\"
'TempFileName = "test"
'With Destwb
'.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
'End With
'Printcode
Sheets(Array("Blad 1")).Select
Sheets("Blad 1").Activate
Application.ActivePrinter = "CutePDF Writer op CPW2:"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"CutePDF Writer op CPW2:", Collate:=True
' This set of code tells the macro to pause for 2 seconds. This will allow for the PDF printer to run through its process and prompt you for a filename.
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 2
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
' This line of code specifies your directory as well as the cell or range which you want the filename to come from.
Filename = "C:\test\test.pdf"
'Filename = "D:\Business Files\PDF Files\" & ActiveSheet.Range("InvNbr").Value & ".pdf"
' This line of code sends the filename characters and the ENTER key to the active application (i.e. the prompt window). The "False" statement allows the macro to continue running without waiting for the keys to be processed.
SendKeys Filename & "{ENTER}", True
'Delete the file you have created
ActiveWorkbook.Close Saved = True
Sourcewb.Activate