【问题标题】:Save copy of workbook as new xlsm, runtime error 1004将工作簿副本另存为新 xlsm,运行时错误 1004
【发布时间】:2018-09-12 13:42:32
【问题描述】:

我正在尝试通过以下代码将工作簿的副本另存为新的 .xlsm 文件:

SaveAs FileName:=StrPadHoofdDocument & "\Docs\" & "\n\" & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

我收到以下错误:“运行时错误 1004:object_Workbook 的对象 SaveAs 方法失败” 我已经阅读了很多具有相同问题的其他主题,但我无法完全解决它。希望大家帮忙!

完整代码:

Sub motivatieFormOpmaken()

Const StBestand = "Stambestand.xlsm"
Const motivatie = "Template motivatieformulier opstapregeling.xlsx"

    Dim wbMotivTemp As Workbook
    Dim wsMotiv As Worksheet
    Dim PathOnly, mot, FileOnly As String
    Dim StrPadSourcenaam As String

    Set wbMotivTemp = ThisWorkbook
    Set wsMotiv = ActiveSheet

    StrHoofdDocument = ActiveWorkbook.Name
    StrPadHoofdDocument = ActiveWorkbook.Path
    StrPadSourcenaam = StrPadHoofdDocument & "\" & c_SourceDump

    If Not FileThere(StrPadSourcenaam) Then
       MsgBox "Document " & StrPadSourcenaam & " is niet gevonden."
    Exit Sub
    End If

    Application.ScreenUpdating = False

    Workbooks.Open FileName:=StrPadSourcenaam
    Application.Run "Stambestand.xlsm!unhiderowsandcolumns"
    Worksheets("stambestand").Activate

    iLaatsteKolom = Worksheets("stambestand").Cells.SpecialCells(xlLastCell).Column
    iLaatsteRij = Worksheets("stambestand").Cells.SpecialCells(xlLastCell).row

    VulKolomNr
    If KolomControle = False Then Exit Sub

    Aantalregels = AantalZichtbareRows
        Dim rng As Range
        Dim row As Range
        Dim StrFileName As String
        'If Aantalregels > 1 Then
         Set rng = Selection.SpecialCells(xlCellTypeVisible)
         For Each row In rng.Rows
           iRijnummer = row.row
           If iRijnummer > 1 Then
              'Windows(c_SourceDump).Activate
              wsMotiv.Range("motiv_cid") = Cells(iRijnummer, iKolomnrCorpID).Text
              wsMotiv.Range("motiv_naam") = Cells(iRijnummer, iKolomnrNaam).Text
              wsMotiv.Range("motiv_ldg") = Cells(iRijnummer, iKolomnrHuidigeLeidingGevende).Text

              n = naamOpmaken

              wbMotivTemp.Activate

              ActiveWorkbook.SaveAs FileName:=StrPadHoofdDocument & "\Docs\" & "\n\" & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
            End If
         Next row

End Sub

Function naamOpmaken() As String
    Dim rng As Range
    Dim row As Range
    Set rng = Selection.SpecialCells(xlCellTypeVisible)

    iRijnummer = rng.row
        If iRijnummer > 1 Then
            s = Cells(iRijnummer, iKolomnrNaam).Text
            Dim Position As Long, Length As Long
            Dim n As String
            Position = InStrRev(s, " ")
            Length = Len(s)
            n = Right(s, Length - Position)
        End If
    naamOpmaken = n
End Function

【问题讨论】:

    标签: vba save runtime-error save-as


    【解决方案1】:

    改变这部分:

    FileName:=StrPadHoofdDocument & "\Docs\" & "\n\" & ".xlsm",
    

    有了这个:

    FileName:=StrPadHoofdDocument & "\Docs\" & n & ".xlsm",
    

    如您所见,问题在于您使用了两次\\。此外,n 是一个变量,它作为字符串传递。在未来的类似情况下,打印有问题的字符串并仔细检查,代码如下:

    Debug.Print StrPadHoofdDocument & "\Docs\" & "\n\" & ".xlsm"
    Debug.Print StrPadHoofdDocument & "\Docs\" & n & ".xlsm"
    

    错误将是可见的。

    【讨论】:

    • 顶,谢谢!我根本没有考虑作为错误的可能来源。供将来参考的好提示。
    猜你喜欢
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多