简体   繁体   English

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

[英]vba save excel worksheet with workbook name plus text as a PDF

This code only saves with the workbook name and not the text "Sample". 此代码仅保存工作簿名称,而不保存文本“ Sample”。 What am I doing wrong? 我究竟做错了什么? All answers appreciated Thanks, Ed 感谢所有答案,谢谢,爱德华

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

You are not telling VBA the filename. 您没有告诉VBA文件名。 Append Filename:= Filename (although I would change the variable name for better reading to, for example, wbFilename : 追加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

For trace with the additional argument: 使用附加参数进行跟踪:

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

Edit: @SJR mentioned your may need to add the extension in Filename 编辑:@SJR提到您可能需要在Filename添加扩展Filename
Also, I've condensed Sheet("Sample").Select / ActiveSheet. 另外,我压缩了Sheet("Sample").Select / ActiveSheet. (which was probably incorrect anyway, I believe it should have been Sheet("Sample").Activate ). (无论如何,这可能都是不正确的,我认为应该是Sheet("Sample").Activate )。

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

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