使用自动文件名将工作表保存为PDF

时间:2013-05-23 08:08:07

标签: excel-vba vba excel

我已经编写了下面的宏,以便正在使用的工作表将打印为PDF,并根据特定单元格的内容填充文件名。

它几乎可以工作,但出于某种原因,每当我运行它时,声明'输出文件夹不存在'。这是因为由于某种原因文件路径加倍

(即:\购买订单\ 2013 \ TEST - Damien \ 2013 Pos \ S:\ Purchase Orders \ 2013 \ TEST - Damien \ 2013 POs \ filename.pdf

Sub SaveToPDF()


' This line of code calls your PDF printer and runs the conversion. Record your own macro to call your PDF printer and copy and paste it here.

ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"Bullzip PDF Printer", Collate:=True

' This set of code tells the macro to pause for 2 seconds. This will allow for the PDF printer to run through its process and prompt you for a filename.

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

' This line of code specifies your directory as well as the cell or range which you want the filename to come from.

FileName = "S:\Purchase Orders\2013\TEST - Damien\2013 POs\" & ActiveSheet.Range("BT12").Value

' This line of code sends the filename characters and the ENTER key to the active application (i.e. the prompt window). The "False" statement allows the macro to continue running without waiting for the keys to be processed.

SendKeys FileName & "{ENTER}", False

End Sub

如果有人能帮我解决这个很棒的代码。

提前致谢。

2 个答案:

答案 0 :(得分:0)

试试这个:

SendKeys "^(a)" & "{DELETE}" & Filename & "{ENTER}", False

删除打印对话框中的旧文件名

ps:在我的测试中它只在我将waitTime设置为5秒时才有效,但可能是我有慢机器

答案 1 :(得分:0)

请参阅Bullzip examples

Set settings = CreateObject("Bullzip.PdfSettings")
settings.SetValue "Output", fileName
settings.WriteSettings True