将工作簿的副本保存为新的xlsm,运行时错误1004

时间:2018-04-03 09:24:51

标签: vba save runtime-error save-as

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

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

我收到以下错误:"运行时错误1004:对象的方法SaveAs of object_Workbook失败" 我已经用同样的问题阅读了很多其他主题,但我无法解决它。希望你们能帮忙!

完整代码:

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

1 个答案:

答案 0 :(得分:1)

更改此部分:

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

有了这个:

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

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

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

然后可以看到错误。