Save excel invoice as xlsm and pdf based on 2 cell values and then print...Solution

TobiaLeviathon

New Member
Joined
Mar 3, 2017
Messages
2
Hi Guys,

I have been working on saving my Invoices in the raw excel format that allows macros, xlsm, and in pdf format to the same folder, having it named automatically by the value of two cells, invoice number and customer name, then printing it out and closing the workbook down. It took a lot of searching to combine these three things, so I thought I'd share it to help anyone who needs it....

This is VBA, I assume you have already 'Inserted' a 'button' from the 'activeX controls' in the 'developer' tab (search google for instructions on this if not)

Here's the code- (bold is the code you need, italics are comments to help understand the code)

Sub SaveandPrint() 'this is the macro's name
Dim FileName As String
Dim Path As String


ActiveSheet.PrintOut 'this bit prints the sheet
Application.DisplayAlerts = False

Path = "C:\add\your\file\destination\here" 'Change the directory path here where you want to save the file
FileName = Range("C18").Value & " " & Range("A7").Value & ".xlsm" 'Change extension here for different excel formats.
'Also, change range cell values to select different cell value for naming
ActiveWorkbook.SaveAs Path & FileName, xlOpenXMLWorkbookMacroEnabled 'Change the format here which matches with the extension above. 'Choose from the following link http://msdn.microsoft.com/en-us/library/office/ff198017.aspx

Application.DisplayAlerts = True

Dim fName As String 'this is the saving to pdf bit
fName = Range("C18").Value & " " & Range("A7").Value 'again, change cells to C18 and A7 to the ones you want to name file

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"C:\add\your\file\destination\here" & fName, Quality:=xlQualityStandard, _
'change destination folder
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

ActiveWorkbook.Close 'closes the workbook

End Sub



Hope this helps and save some lots of time, took me bloody ages to wrap my head round it and implement it!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,214,660
Messages
6,120,787
Members
448,994
Latest member
rohitsomani

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