我有一个包含项目信息的主Excel文件。在包含图形的主文件所在的文件夹中,总是会有另一个名为"Job export“的excel文件-每个图形都在不同的工作表上。绘图将从图纸2开始,在图纸n-1处结束(目前代码还将打印图纸1和图纸n)。
我已经找到并修改了VBA代码,它将这些图纸从每个工作表打印到单独的PDF文件中,并在每个工作表的单元格C5中命名。但这意味着我必须打开包含图纸的excel文件,并将代码复制到该工作簿中,以便打印PDF。我想将此代码添加到我的主Excel中,以便它在后台打开“作业导出”excel,并将图纸打印到单独的PDF中。
进一步的步骤是根据客户在Cell B7中的名称和B11及以后版本中的零件编号将这些PDF保存到文件夹中(在这种情况下,保存的PDF也将以零件编号命名)。
使用VBA可以做到这一点吗?
编辑:
我一直在更改打印的PDF的保存位置。我试着在文件夹"C:\suvaline“中硬编码,但不能将它们保存在那里。正如我之前所写的,保存文件夹应该是: C:\suvaline\"Clients name"\"Part number“,但它甚至不能与硬编码文件夹一起工作,所以我甚至没有尝试从Excel中获取客户端名称。我是不是漏掉了什么?
Sub ExportToPDFFromClosed()
Dim ws As Worksheet
Dim wbA As Workbook
Dim strPath
Dim strFile
Dim wb2 As Workbook
i = 11
j = 2
sPath = "C:\suvaline"
Application.ScreenUpdating = False
Set wbA = ActiveWorkbook
strPath = wbA.path
strPath = strPath & "\"
Set wbA = Workbooks.Open(strPath & "Job export pic3")
Set wb2 = ThisWorkbook
For Each ws In Worksheets
ws.Select
nm = wb2.Sheets("Prep+BOM").Cells(i, j).Value _
strFile = nm & ".pdf"
strPathFile = "C:\suvaline" & strFile
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:=strPathFile & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
i = i + 1
Next ws
wbA.Close
End Sub发布于 2019-01-25 20:35:10
这是为我工作的最后一段代码。绝对不是最优雅的:
Sub ExportToPDFFromClosed()
Dim ws As Worksheet
Dim wbA As Workbook
Dim strPath
Dim strFile
Dim wb2 As Workbook
Dim strPath2
Dim saveDest
i = 11
j = 2
Application.ScreenUpdating = False
Set wbA = ActiveWorkbook
strPath = wbA.path
strPath = strPath & "\"
Set wbA = Workbooks.Open(strPath & "Job export pic3")
Set wb2 = ThisWorkbook
strPath2 = wb2.Sheets("Prep+BOM").Range("B7")
saveDest = "C:\suvaline\" & strPath2 & "\"
For Each ws In Worksheets
ws.Select
nm = wb2.Sheets("Prep+BOM").Cells(i, j).Value _
strFile = nm & ".pdf"
strPathFile = saveDest & nm & "\" & strFile
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:=strPathFile & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
i = i + 1
Next ws
wbA.Close
End Subhttps://stackoverflow.com/questions/54324518
复制相似问题