snuggles57
New Member
- Joined
- Oct 9, 2017
- Messages
- 14
Hello I am in need of some assistance with a Macro the EDGE chat thing has generated.
The Macro is supposed to combine tif images located in a nominated folder and convert and combine the images to single pdf. The macro generates the pdf however excel continues to (run) spin I am wondering if there is a line of code missing that stops the loop. I have to do CTRL ALT DEL and kill excel to make it stop.
Also I am a bit unsure if I am supposed to change anything other than the folderPath. Greatly appreciate any help. I did do a search but could find no specific results.
The Macro is supposed to combine tif images located in a nominated folder and convert and combine the images to single pdf. The macro generates the pdf however excel continues to (run) spin I am wondering if there is a line of code missing that stops the loop. I have to do CTRL ALT DEL and kill excel to make it stop.
Also I am a bit unsure if I am supposed to change anything other than the folderPath. Greatly appreciate any help. I did do a search but could find no specific results.
VBA Code:
Sub PrintTifToPdf()
'Declare variables
Dim FolderPath As String
Dim FileName As String
Dim PdfName As String
Dim ShellApp As Object
Dim PdfFiles As Object
'Set the folder path where the tif images are located
FolderPath = "G:\Test"
'Create a new instance of the Shell.Application object
Set ShellApp = CreateObject("Shell.Application")
'Loop through all the tif files in the folder
FileName = Dir(FolderPath & "*.tif")
Do While FileName <> "*.tif"
'Set the pdf file name based on the tif file name
PdfName = Replace(FileName, ".tif", ".pdf")
'Print the tif file to pdf using the Microsoft Print to PDF printer
Shell "rundll32.exe C:\WINDOWS\system32\shimgvw.dll,ImageView_PrintTo /pt """ & FolderPath & FileName & """ ""Microsoft Print to PDF"" ""Microsoft Print to PDF""", vbHide
'Wait for the pdf file to be created
Do While Dir(FolderPath & PdfName) = ""
Application.Wait Now + TimeValue("0:00:01")
Loop
'Move to the next tif file
FileName = Dir()
Loop
'Get all the pdf files in the folder as a collection
Set PdfFiles = ShellApp.Namespace(FolderPath).Items
'Filter the collection to include only pdf files
PdfFiles.Filter 64, "*.pdf"
'Combine all the pdf files into a single pdf file using the Shell.Application object
ShellApp.Namespace(FolderPath & "Combined.pdf").CopyHere PdfFiles
'Wait for the combined pdf file to be created
Do While Dir(FolderPath & "Combined.pdf") = ""
Application.Wait Now + TimeValue("0:00:01")
Loop
'Release the objects
Set PdfFiles = Nothing
Set ShellApp = Nothing
'Inform the user that the macro is done
MsgBox "All tif images have been printed and combined into a single pdf file.", vbInformation, "Macro Done"
End Sub
Last edited by a moderator: