打开另一个现有的Excel文件 - 添加数据然后重新保存然后保存PDF

时间:2015-10-05 16:23:17

标签: excel-vba vba excel

所以我有一个VBA项目,我想要做的是从我的用户表单打开一个现有文件(Pending Data Input)。添加从我的用户表单收集的数据,打开添加新数据,然后保存,然后保存PDF。这就是我想出来的,我不想看到excel,因为excel应该隐藏所有我想看到的是我的表格

Sub OpenDocument4()
Dim wsh As Worksheet
Dim wb As Workbook
Dim wb1 As Workbook
Set wb1 = ActiveWorkbook
Dim i As Long
Dim lngEndRowInv As Long
Dim FileName1, FileName2 As String
Dim var1
Dim var2
Dim var3
var1 = frmsetup.tbIssuesRear60.Caption
var2 = frmsetup.tbActionsRear60.Caption
var3 = frmsetup.tbOwnerRear60.Caption
Application.Visible = False
Application.ScreenUpdating = False
FileName2 = "C:\MiltonAuditAppsCenter\SEAT AUDIT\TEMP DOCUMENTS\" & "SEQ-" &           frmsetup.lblsequence.Caption & " " & frmsetup.lbldate.Caption & ".xlsm"
FileName1 = "SEQ-" & frmsetup.lblsequence.Caption & " " & frmsetup.lbldate.Caption & ".pdf"

Set wb = Workbooks.Open(FileName2)
Set wsh = wb.Worksheets("ACTIONS")
Worksheets("ACTIONS").Range("E20").Value = var1
Worksheets("ACTIONS").Range("E22").Value = var2
Worksheets("ACTIONS").Range("J21").Value = var3
Application.Visible = False
Application.ScreenUpdating = False
With Sheets(Array("END RESULTS", "DRIVER SEAT", "PASSENGER SEAT", "40% SEAT", "60% SEAT", "RSC SEAT", "ACTIONS")).Select
 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
     FileName:="H:\APPLICATIONS\SEAT AUDIT\QUERY RESULTS\SEAT AUDIT - PDF\" & FileName1, _
     Quality:=xlQualityStandard, _
     IncludeDocProperties:=True, IgnorePrintAreas:=False,     OpenAfterPublish:=False
End With
wb.Close savechanges:=False

End Sub

1 个答案:

答案 0 :(得分:1)

看起来它应该可以正常工作,但是对于Workbook对象进行明确处理可能没什么坏处,因为您已经Worksheets("ACTIONS")已经wsh Worksheets("ACTIONS").Range(...).Value Sub OpenDocument1() Dim wsh As Worksheet Dim wb as Workbook '## The workbook you want to open Dim i As Long Dim lngEndRowInv As Long Dim FileName As String Application.Visible = False Application.ScreenUpdating = False FileName = "C:\MiltonAuditAppsCenter\SEAT AUDIT\TEMP DOCUMENTS\" & "SEQ-" & frmsetup.lblsequence.Caption & " " & frmsetup.lbldate.Caption & ".xlsm" Set wb = Workbooks.Open(FileName) Set wsh = wb.Worksheets("ACTIONS") wsh.Range("E20").Value = frmReviewPDFPending.tbIssuesRear60.Text wsh.Range("E22").Value = frmReviewPDFPending.tbActionsRear60.Text wsh.Range("J21").Value = frmReviewPDFPending.tbOwnerRear60.Text wb.Sheets(Array("END RESULTS", "DRIVER SEAT", "PASSENGER SEAT", "40% SEAT", "60% SEAT", "RSC SEAT", "ACTIONS")).ExportAsFixedFormat Type:=xlTypePDF, FileName:= _ "H:\APPLICATIONS\SEAT AUDIT\QUERY RESULTS\SEAT AUDIT - PDF\" & FileName1, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _ False wb.Close savechanges:=True Application.Visible = True Application.ScreenUpdating = True End Sub 变量,你应该使用它来代替硬编码ExportAsFixedFormat赋值。

 With Sheets(Array("END RESULTS", "DRIVER SEAT", "PASSENGER SEAT", "40% SEAT", "60% SEAT", "RSC SEAT", "ACTIONS")).Select
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
         FileName:= "H:\APPLICATIONS\SEAT AUDIT\QUERY RESULTS\SEAT AUDIT - PDF\" & FileName1, _
         Quality:=xlQualityStandard, _
         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= False
 End With

注意:如果您在使用{{1}}时遇到问题,请回复原始方法:

{{1}}