简体   繁体   English

另存为文件,不提示输入文件名

[英]Save as file without prompt for file name

I'm trying to create a PDF file without any user interface, other than clicking a button within the Excel file. 我正在尝试创建一个没有任何用户界面的PDF文件,只是单击Excel文件中的一个按钮。

Using the below code, when the file is to be saved as with an automatically generated name, the code prompts for the filename, instead of grabbing it from the code. 使用以下代码,当使用自动生成的名称将文件另存为时,代码提示输入文件名,而不是从代码中获取文件名。

I have a feeling that sendkeys is not working out. 我感觉sendkeys无法正常工作。

Sub PrinttoPDFTest()

    ActiveSheet.PageSetup.PrintArea = "$A$1:$F$17"
    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    ActiveSheet.PageSetup.PrintArea = "$A$1:$F$17"
    With ActiveSheet.PageSetup
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With

    ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Adobe PDF on Ne04:", Collate:=True

    newHour = Hour(Now())
    newMinute = Minute(Now())
    newSecond = Second(Now()) + 5
    waitTime = TimeSerial(newHour, newMinute, newSecond)
    Application.Wait waitTime

    Filename = "C:\Temp\PDF\" & ActiveSheet.Range("DateSerial").Value & ".pdf"

    SendKeys Filename & "{Enter}", False

End Sub

It would be simpler to use .ExportAsFixedFormat rather than .PrintOut, and just stack your requests (no delay code needed): 使用.ExportAsFixedFormat而不是.PrintOut会更简单,只需堆叠您的请求即可(无需延迟代码):

Sub ExporttoPDF()

    Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, "C:\Folder\Filename1.pdf"
    Sheets("Sheet2").ExportAsFixedFormat xlTypePDF, "C:\Folder\Filename2.pdf"

End Sub

Just replace the destinations and filenames, and this code should work fine. 只需替换目标位置和文件名,此代码就可以正常工作。 If you want to use a value in the sheet as part of a filename, then you can concatenate (&) like this: 如果要在工作表中使用值作为文件名的一部分,则可以这样连接(&):

Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, "C:\Folder\" & Range("P10").Value & ".pdf"

Or store the filename in a variable and do it this way: 或者将文件名存储在变量中,并通过以下方式进行操作:

dim filename as string

filename = Range("P10").Value

Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, filename

And it goes on and on... 并持续下去...

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

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