简体   繁体   English

我如何通过VBA另存为pdf

[英]How do i save as pdf via VBA

I have a code, where I want to: 我有一个代码,我想在哪里:

  1. Save Masterfile (current active workbook), 保存主文件(当前活动的工作簿),
  2. amend the workbook and delete sheets, 修改工作簿并删除工作表,
  3. then save separate copies of the edited workbook as an Excel sheet and a PDF file. 然后将编辑后的工作簿的单独副本另存为Excel工作表和PDF文件。

The problem I have here is that the code saves the PDF file as the original Masterfile even after I have tried to activate the edited Excel file. 我在这里遇到的问题是,即使我试图激活编辑的Excel文件,该代码也将PDF文件保存为原始Masterfile。 Any help here? 这里有什么帮助吗? Would really appreciate any advice! 非常感谢任何建议! Code below: 代码如下:

ActiveWorkbook.Save


Sheets("Inventory").Select
Cells.Select
Selection.Copy
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.DisplayAlerts = False

Sheets("May").Select
Cells.Select
Selection.Copy
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Range("A1").Select
Sheets("Macro").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = False

Sheets("Oct").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = False

Sheets("Inventory").Select
Range("A1").Select

Sheets("Inventory").Cells.Interior.ColorIndex = 0

ChDir "G:\9Fixed\Posi\2016\Inventory"
ActiveWorkbook.SaveAs Filename:= _
    "G:\9Fixed\Posi\2016\Inventory\Asia Fixed - " & Format(Date, "dd mmm") & ".xls", FileFormat:= _
    xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = False
'ActiveWorkbook.ExclusiveAccess
Application.DisplayAlerts = True

Workbooks("Asia  - " & Format(Date, "dd mmm") & ".xls").Activate
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"G:\9Fixed Income\Positions\2016\Inventory\Asia Fixed Income - " & Format(Date, "dd mmm") & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False

I could not reproduce the issue you were having. 我无法重现您遇到的问题。

Here is my 'test' code, using a workbook with 4 sheets and information in cells(1,1), one sheet named "May" --- which is deleted, and the new file does not have "May" and the pdf does not either. 这是我的“测试”代码,使用一个工作簿,该工作簿有4张工作表,并在cells(1,1)中包含信息,其中一张名为“ May”的工作表---已删除,而新文件没有“ May”和pdf也没有。

I moved the saveas code towards the top of the file. 我将saveas代码移到文件顶部。 Maybe that will fix your issue, but I don't believe it will. 也许这可以解决您的问题,但我认为不会。

Option Explicit
Sub SaveCopies()
    Dim c_OUTPUTDIR As String
    Dim sFileName As String
    Dim fso As Object ' Used to handle paths, filenames, etc.
    Set fso = CreateObject("Scripting.FileSystemObject")
    c_OUTPUTDIR = "C:\temp\"
    ' Save the master copy.
    ActiveWorkbook.Save


    ' Generate new name for file.
    sFileName = fso.GetBaseName(ActiveWorkbook.FullName) & "_" & Format(Date, "dd mmm")
    ' Save new working file.
    ActiveWorkbook.SaveAs Filename:= _
        c_OUTPUTDIR & sFileName & ".xls", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False

    ' Make changes to working file.
    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets("May").Delete
    Application.DisplayAlerts = True
    ' Save the changes.
    ActiveWorkbook.Save

    ' Save a PDF of the file.
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    c_OUTPUTDIR & sFileName & ".pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM