Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用自动文件名将工作表保存为PDF_Excel_Vba - Fatal编程技术网

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

Excel 使用自动文件名将工作表保存为PDF,excel,vba,Excel,Vba,我编写了下面的宏,以便使用中的工作表将打印为PDF格式,并根据特定单元格的内容填充文件名 它几乎可以工作,但出于某种原因,每当我运行它时,它都会声明“输出文件夹不存在”。这是因为出于某种原因,文件路径加倍了 (即S:\Purchase Orders\2013\TEST-Damien\2013 Pos\S:\Purchase Orders\2013\TEST-Damien\2013 Pos\filename.pdf Sub SaveToPDF() ' This line of code cal

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

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

(即S:\Purchase Orders\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
如果有人能帮我修复这个代码那就太好了

提前感谢。

试试这个:

SendKeys "^(a)" & "{DELETE}" & Filename & "{ENTER}", False
它将删除“打印”对话框中的旧文件名

ps:在我的测试中,只有当我将waitTime设置为5秒时,它才起作用,但可能是因为我的机器速度较慢


使用以下链接中的
CreateFolder
功能,该功能将创建唯一的文件名并确保文件夹存在。您可能需要调整。Shipormot-我复制并粘贴了您的代码并运行了它,我让它第二次运行,但我发现文件路径(即S:\Purchase Orders\2013\TEST-Damien\2013 POs)正在我的工作表中随机粘贴。我想你还没有遇到过这个问题。我不是最擅长宏的(只在过去3周内尝试过)所以任何指导都是很好的。Thanks@Mortend,若答案解决了问题,请接受它。第二个问题呢,我建议你们再发一个问题。但在考虑它的表述之前,因为不清楚你们想要实现什么。我认为你们需要一些逻辑来找到表格上有文件名的单元格。最简单的解决方案是固定包含文件名的单元格或包含文件名的单元格坐标。但同样,不清楚最后想要的是什么。
Set settings = CreateObject("Bullzip.PdfSettings")
settings.SetValue "Output", fileName
settings.WriteSettings True