Modify File before saving it.

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
784
Office Version
  1. 365
Hi,

found very helpful code to print multiples attachments in outlook, just wonder if can prompt to when open file to fix like columns showing ##### because is narrow after modifying saving it.

here is the code ( and after will put a sample that you can see that date cannot see it because columns no wide enough is excel file converted to pdf)


VBA Code:
Sub BatchPrintAllAttachmentsinMultipleEmails()
    Dim objFileSystem As Object
    Dim strTempFolder As String
    Dim objSelection As Outlook.Selection
    Dim objItem As Object
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objAttachment As Outlook.Attachment
    Dim objShell As Object
    Dim objTempFolder As Object
    Dim objTempFolderItem As Object
    Dim strFilePath As String
 
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    strTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Attachments " & Format(Now, "YYYY-MM-DD_hh-mm-ss")
    'Create a new temp folder
    MkDir (strTempFolder)
 
    Set objSelection = Outlook.Application.ActiveExplorer.Selection
 
    For Each objItem In objSelection
        If TypeOf objItem Is MailItem Then
           Set objMail = objItem
           Set objAttachments = objMail.Attachments
 
           'Save all the attachments in the temp folder
           For Each objAttachment In objAttachments
               strFilePath = strTempFolder & "\" & objAttachment.FileName
               objAttachment.SaveAsFile (strFilePath)
 
               'Print all the files in the temp folder
               Set objShell = CreateObject("Shell.Application")
               Set objTempFolder = objShell.NameSpace(0)
               Set objTempFolderItem = objTempFolder.ParseName(strFilePath)
               objTempFolderItem.InvokeVerbEx ("print")
           Next objAttachment
        End If
    Next
End Sub

Sample:

DATE COLUMNS NO VISIBLE.PNG
 
Hi,

Can it possible to change to prompt me for saving location instead of fix one?

thank you,
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
When the SaveAs prompt is displayed and the user is prompted to choose a name, the user also has the opportunity to choose a location other than the default one. Doesn't this suffice?
 
Upvote 0
Hi,

When I run it getting "Run-time error '1004': Method 'This workbook of object, failed and highlight this line:

VBA Code:
saveAsPdfFilename = Application.GetSaveAsFilename(InitialFileName:=ThisWorkbook.Path, FileFilter:="PDF File (*.pdf), *.pdf", Title:="Save As")

Please note:

I change this line to the folder I created "Attachments"

VBA Code:
strTempFolder = objFileSystem.GetSpecialFolder(2).Path & [B][COLOR=rgb(226, 80, 65)]"\Attachments "[/COLOR][/B] & Format(Now, "YYYY-MM-DD_hh-mm-ss")


thank you
 
Upvote 0
Try changing the InitialFilename argument as follows . . .

VBA Code:
saveAsPdfFilename = Application.GetSaveAsFilename(InitialFileName:=strTempFolder, FileFilter:="PDF File (*.pdf), *.pdf", Title:="Save As")

Does this help?
 
Upvote 0
changed it now giving me this error:

Run-Time error '438' object doesn't support property or method
 
Upvote 0
Are you running your code within Excel or Outlook?

When the error occurs, what exactly does it highlight?
 
Upvote 0
sorry missed 2nd question, it highlights this one:


VBA Code:
aveAsPdfFilename = Application.GetSaveAsFilename(InitialFileName:=strTempFolder, FileFilter:="PDF File (*.pdf), *.pdf", Title:="Save As")
 
Upvote 0
That would explain why you're getting the error. But if that's the case, you should have received a user-defined type compile error highlighting this line . . .

VBA Code:
Dim wb as Workbook

Did you not get an error?
 
Upvote 0
Try the following code instead . . .

VBA Code:
Sub BatchPrintAllAttachmentsinMultipleEmails()
    Dim objFileSystem As Object
    Dim strTempFolder As String
    Dim objSelection As Outlook.Selection
    Dim objItem As Object
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objAttachment As Outlook.Attachment
    Dim objShell As Object
    'Dim objTempFolder As Object
    'Dim objTempFolderItem As Object
    Dim strFilePath As String
    Dim saveAsPdfFilename As Variant
    Dim xl As Object
    Dim wb As Object
    
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    strTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Attachments " & Format(Now, "YYYY-MM-DD_hh-mm-ss")
    'Create a new temp folder
    MkDir (strTempFolder)
    
    Set xl = CreateObject("Excel.Application")
 
    Set objSelection = Outlook.Application.ActiveExplorer.Selection
 
    For Each objItem In objSelection
        If TypeOf objItem Is MailItem Then
           Set objMail = objItem
           Set objAttachments = objMail.Attachments
           For Each objAttachment In objAttachments
              If LCase(Right(objAttachment.FileName, 4)) = ".csv" Or LCase(Right(objAttachment.FileName, 5)) = ".xlsx" Then
                    saveAsPdfFilename = xl.GetSaveAsFilename(InitialFileName:=strTempFolder, FileFilter:="PDF File (*.pdf), *.pdf", Title:="Save As")
                    If saveAsPdfFilename = False Then Exit Sub
                    strFilePath = strTempFolder & "\" & objAttachment.FileName
                    objAttachment.SaveAsFile strFilePath
                    Set wb = xl.Workbooks.Open(strFilePath)
                    wb.worksheets(1).Columns("A:B").AutoFit
                    wb.ExportAsFixedFormat 0, saveAsPdfFilename '0 = xlTypePDF
                    wb.Close SaveChanges:=False
                    Exit For
              End If
           Next objAttachment
        End If
    Next
End Sub

Note, however, you can instead use the Environ function to retrieve the path to your temporary folder . . .

VBA Code:
strTempFolder = Environ("temp") & "\Temp for Attachments " & Format(Now, "YYYY-MM-DD_hh-mm-ss")

And, since you're running your within Outlook, the following would suffice . . .

VBA Code:
Set objSelection = Application.ActiveExplorer.Selection

So the code can be re-written as follows . . .

VBA Code:
Sub BatchPrintAllAttachmentsinMultipleEmails()
    Dim objFileSystem As Object
    Dim strTempFolder As String
    Dim objSelection As Outlook.Selection
    Dim objItem As Object
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objAttachment As Outlook.Attachment
    Dim objShell As Object
    Dim strFilePath As String
    Dim saveAsPdfFilename As Variant
    
    'Excel objects
    Dim xl As Object
    Dim wb As Object
    
    strTempFolder = Environ("temp") & "\Temp for Attachments " & Format(Now, "YYYY-MM-DD_hh-mm-ss")
    
    'Create a new temp folder
    MkDir (strTempFolder)
    
    Set xl = CreateObject("Excel.Application")
 
    Set objSelection = Application.ActiveExplorer.Selection
 
    For Each objItem In objSelection
        If TypeOf objItem Is MailItem Then
           Set objMail = objItem
           Set objAttachments = objMail.Attachments
           For Each objAttachment In objAttachments
              If LCase(Right(objAttachment.FileName, 4)) = ".csv" Or LCase(Right(objAttachment.FileName, 5)) = ".xlsx" Then
                    saveAsPdfFilename = xl.GetSaveAsFilename(InitialFileName:=strTempFolder, FileFilter:="PDF File (*.pdf), *.pdf", Title:="Save As")
                    If saveAsPdfFilename = False Then Exit Sub
                    strFilePath = strTempFolder & "\" & objAttachment.FileName
                    objAttachment.SaveAsFile strFilePath
                    Set wb = xl.Workbooks.Open(strFilePath)
                    wb.worksheets(1).Columns("A:B").AutoFit
                    wb.ExportAsFixedFormat 0, saveAsPdfFilename '0 = xlTypePDF
                    wb.Close SaveChanges:=False
                    Exit For
              End If
           Next objAttachment
        End If
    Next
    
    Set xl = Nothing
End Sub

Does this help?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top