作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我遇到的问题。
我创建了一个 Excel 项目。在我的项目中,我将当前工作簿保存为 PDF,并使用日志信息和 PDF 的超链接更新现有工作簿( Step1 )。
在项目的另一点,我将项目重新保存为同一个 PDF(以覆盖现有 PDF)。此时我尝试重新保存收到的 PDF:
run-time error-1004
Sub Step1()
Dim rng As Range
Dim nwb As Workbook
Dim FileName As String
Dim var
Dim var1
Dim var2
Dim var3
Dim var4
Dim var5
Dim var6
var1 = frmsetup.cmbauditor.Text
var2 = frmsetup.lblsequence.Caption
var3 = frmsetup.cmbtrimstyle.Text
var = "SEQ-" & frmsetup.lblsequence.Caption & " "
var4 = frmsetup.lbldate.Caption
FileName = var & var4
With Sheets(Array("END RESULTS", "DRIVER SEAT", "PASSENGER SEAT", "40% SEAT", "60% SEAT", "RSC SEAT")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"H:\APPLICATIONS\SEAT AUDIT\QUERY RESULTS\SEAT AUDIT - PDF\" & FileName & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End With
Application.ScreenUpdating = False
Application.WindowState = xlMaximized
Set nwb = Workbooks.Open("H:\APPLICATIONS\SEAT AUDIT\LOG FILES\Seat Audit Log.xlsm")
With Sheets("Seat Audit Log")
nextrow = Range("B" & Rows.Count).End(xlUp).Row + 1
Cells(nextrow, 1).Value = var1
Cells(nextrow, 2).Value = var2
Cells(nextrow, 3).Value = var3
Cells(nextrow, 4).Value = var4
Set rng = .Range("E" & nextrow)
rng.Parent.Hyperlinks.Add Anchor:=rng, Address:="H:\APPLICATIONS\SEAT AUDIT\QUERY RESULTS\SEAT AUDIT - PDF\" & FileName & ".pdf", TextToDisplay:="CLICK HERE!"
End With
Application.ScreenUpdating = True
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
Sub Step2()
Dim FileName As String
Dim var
Dim var4
var = "SEQ-" & frmsetup.lblsequence.Caption & " "
var4 = frmsetup.lbldate.Caption
FileName = var & var4
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\" & FileName & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End With
End Sub
最佳答案
您的大部分 With
block 似乎构造不佳,可能会导致一些错误(或者,也许不是,很难确定)。通常,您会将对象限定为 With
目的。就您而言,除了一两行代码外,您似乎没有这样做。
您不需要变量来表示表单控件,这样做会使您的代码更难阅读。我已经相应地修改了 Step1。
但是,主要问题是:通常当您尝试 Save
一个文件,你首先需要检查同一个文件是否已经存在,如果存在,删除它。
Sub Step1()
Dim rng As Range
Dim nwb As Workbook
Dim FileName As String
FileName = "SEQ-" & frmsetup.lblsequence.Caption & " " & frmsetup.lbldate.Caption
'## Add the PATH and EXTENSION to the filename
FileName = "H:\APPLICATIONS\SEAT AUDIT\QUERY RESULTS\SEAT AUDIT - PDF\" & FileName & ".pdf"
With Sheets(Array("END RESULTS", "DRIVER SEAT", "PASSENGER SEAT", "40% SEAT", "60% SEAT", "RSC SEAT")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End With
Application.ScreenUpdating = False
Application.WindowState = xlMaximized
**'This is the next part of the step1, it opens a existing workbook and adds log information including hyperlink to pdf'**
Set nwb = Workbooks.Open("H:\APPLICATIONS\SEAT AUDIT\LOG FILES\Seat Audit Log.xlsm")
With Sheets("Seat Audit Log")
nextrow = Range("B" & .Rows.Count).End(xlUp).Row + 1
.Cells(nextrow, 1).Value = frmsetup.cmbauditor.Text
.Cells(nextrow, 2).Value = frmsetup.lblsequence.Caption
.Cells(nextrow, 3).Value = frmsetup.cmbtrimstyle.Text
.Cells(nextrow, 4).Value = frmsetup.lbldate.Caption
.Set rng = .Range("E" & nextrow)
rng.Parent.Hyperlinks.Add Anchor:=rng, Address:=FileName, TextToDisplay:="CLICK HERE!"
End With
Application.ScreenUpdating = True
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
Sub Step2()
Dim FileName As String
FileName = "SEQ-" & frmsetup.lblsequence.Caption & " " & frmsetup.lbldate.Caption
'## Add the PATH and EXTENSION to the filename
FileName = "H:\APPLICATIONS\SEAT AUDIT\QUERY RESULTS\SEAT AUDIT - PDF\" & FileName & ".pdf"
'## Check to see if this file exists, and delete it if it does
If Dir(FileName) <> vbNullString Then
Kill FileName
End If
With Sheets(Array("END RESULTS", "DRIVER SEAT", "PASSENGER SEAT", "40% SEAT", "60% SEAT", "RSC SEAT", "ACTIONS")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End With
End Sub
关于Excel另存为PDF并覆盖现有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32864662/
我有 json 数据: { "products": [ { "productId" : 0, "productImg" : "../img/product-ph
我是一名优秀的程序员,十分优秀!