根据2个单元格值将excel发票保存为xlsm和pdf,然后打印...解决方案

时间:2017-03-21 13:12:00

标签: excel vba excel-vba pdf

我的目标 - 在excel发票上有一个按钮,一旦按下将打印发票,保存到xlsm,保存为pdf(两个保存从两个单元格中取自动名称,发票号和客户名称)然后关闭在工作簿上。

我已经搜索过低和高的答案,但只是在单独的元素中找到了帮助。

文件名所需的单元格是C18和A7。

1 个答案:

答案 0 :(得分:1)

感谢Peh教我正确的礼仪。以下是我的问题的答案 -

这里是代码 - (请阅读标有'以了解每个位的内容的评论)

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/libr.../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

希望这会有所帮助,并节省一些时间,带着我的血腥岁月来绕过它并实施它!