excel - Excel 2019 VBA SaveAs 生成运行时 1004
问题描述
我想将 Excel 文件保存到 XLMS。
在 office 2010 - 2016 中,此代码有效,但在 2019 年我收到运行时错误 1004。
我在互联网上搜索了解决方案。
我在 SaveAs 行收到错误。
Dim Wb As Workbook
Dim NewFileName As String
Dim NewFileFilter As String
Dim myTitle As String
Dim FileSaveName As Variant
Dim NewFileFormat As Long
Set Wb = ThisWorkbook
NewFileName = "Test.xlsm"
NewFileFilter = "Excel Macro-Enabled workbook (*.xlsm), *.xlsm"
NewFileFormat = 52
myTitle = "Navigate to the required folder"
FileSaveName = Application.GetSaveAsFilename _
(InitialFileName:=NewFileName, _
filefilter:=NewFileFilter, _
Title:=myTitle)
If Not FileSaveName = False Then
Wb.SaveAs FileName:=FileSaveName, _
FileFormat:=NewFileFormat
'Call PDF_PRINT
Else
'MsgBox "File NOT Saved. User cancelled the Save."
X = MsgBox("ExcelFile NOT Saved.User cancelled the Save." & vbCrLf & _
"Click CANCEL to abort pdf-save function as well." & vbCrLf & _
"Or click OK to proceed with the pdf-saving function.", vbOKCancel, _
"EXCEL SAVE FUNCTION")
If X = vbOK Then PDF_PRINT Else Exit Sub
End If
解决方案
请试试这个,它正在为我保存 PDF。
Sub MacroTest()
Dim Wb As Workbook
Dim NewFileName As String
Dim NewFileFilter As String
Dim myTitle As String
Dim FileSaveName As Variant
Dim NewFileFormat As Long
Set Wb = ThisWorkbook
NewFileName = "Test.xlsm"
NewFileFilter = "Excel Macro-Enabled workbook (*.xlsm), *.xlsm"
NewFileFormat = 52
myTitle = "Navigate to the required folder"
FileSaveName = Application.GetSaveAsFilename _
(InitialFileName:=NewFileName, _
filefilter:=NewFileFilter, _
Title:=myTitle)
If Not FileSaveName = False Then
Wb.SaveAs Filename:=FileSaveName, _
FileFormat:=NewFileFormat
Call PDF_Print
Else
'MsgBox "File NOT Saved. User cancelled the Save."
X = MsgBox("ExcelFile NOT Saved.User cancelled the Save." & vbCrLf & "Click CANCEL to abort pdf-save function as well." & vbCrLf & "Or click OK to proceed with the pdf-saving function.", vbOKCancel, "EXCEL SAVE FUNCTION")
End If
End Sub
Sub PDF_Print()
Dim strPth As String, strFile As String
strPth = "C:\"
strFile = "Test.pdf"
'If X = vbOK Then PrintPDF Else Exit Sub
If X = vbOK Then PrintPDF(0, strPth & strFile) Then
MsgBox "Printing failed"
End If
End Sub
推荐阅读
- r - 如何对 data.frame 中不同级别的因子应用相同的函数
- plotly - 如何在绘图中修改 Y 轴刻度以适应具有不同范围的图形?
- java - 2个方向键的JavaFX组合键问题
- windows - 如何使用类似于 requirements.txt 的内容通过 Chocolatey 包安装 Forloop
- angular - Angular Nebular nbInput - 将掩码添加到输入字段中
- c - 在c中从/向共享内存读取和写入int
- android-studio - 如何使用 RecyclerView 从 CardView 中的每个 Card 实现多个意图?
- excel - 我们可以使用范围查找来搜索多个单词吗?分裂()
- javascript - javascript - 点击事件目标排除孩子
- twilio - 来自 Safari iPhone 的远程视频显示黑屏