首页 > 解决方案 > 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

标签: excelvbaexcel-2019

解决方案


请试试这个,它正在为我保存 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

推荐阅读