VBA将Excel工作表以及工作簿名称和文本另存为PDF

时间:2019-02-12 15:58:39

标签: excel vba

此代码仅保存工作簿名称,而不保存文本“ Sample”。我究竟做错了什么?所有答案表示赞赏 谢谢, 埃德

Sub SamplePDF()
Dim strFolder As String
    Dim i As Long

    'Find the position of the period in the file name
    i = InStr(ActiveWorkbook.Name, ".")

    Filename = Left(ActiveWorkbook.Name, i - 1) & "Sample"
    Sheets("Sample").Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Quality _
        :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
End Sub

2 个答案:

答案 0 :(得分:1)

您没有告诉VBA文件名。附加Filename:= Filename(尽管我会为了更好地读取而将变量名更改为wbFilename

i = InStr(ActiveWorkbook.Name, ".")

wbFilename = Left(ActiveWorkbook.Name, i - 1) & "Sample"
Sheets("Sample").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Quality _
    :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True, Filename:=wbFilename

答案 1 :(得分:1)

使用附加参数进行跟踪:

Filename = Left(ActiveWorkbook.Name, i - 1) & "Sample.pdf"
Sheets("Sample").ExportAsFixedFormat Type:=xlTypePDF, Quality _
    :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True, Filename:=FileName

编辑:@SJR提到您可能需要在Filename中添加扩展名
另外,我已经压缩Sheet("Sample").Select / ActiveSheet.(无论如何这可能是不正确的,我认为应该是Sheet("Sample").Activate)。