Excel 365 SaveAs VBA issue

Viewed 35

I've been using this macro for quite some time now and had no issues with it while on excel 2019.

Recently I've changed over to office 365 and I'm getting an error with this line of code It'll save the first file and then after that it gets a "Method SaveAs of object _workbook failed"

With Destwb
                    .SaveAs foldername _
                          & "\" & DateString & Destwb.Sheets(1).Name & FileExtStr, _
                            FileFormat:=FileFormatNum
                End With

This is the whole code from my excel

 Sub Split_To_Workbook_and_Email()
'Working in 2013/2016
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim sh As Worksheet
    Dim DateString As String
    Dim foldername As String
    Dim myOutlook As Object
    Dim myMailItem As Object
    Dim mySubject As String
    Dim myPath As String
    Dim ColHead As String
    Dim SigString As String
    Dim Signature As String
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With

    
    'Prompt for Email Subject

    Set otlApp = CreateObject("Outlook.Application")
    
    mySubject = InputBox("Subject for Email", "Save and Email", "Credit subject - " & Format(Date, "mmm yyyy"))
    If StrPtr(mySubject) = 0 Then
        MsgBox ("         Cancelled")
    ElseIf mySubject = vbNullString Then
        MsgBox ("          Nothing was entered")
        Else
            
    Worksheets("Macro").Visible = False
    Worksheets("Credits").Visible = False
    Worksheets("Email List").Visible = False
    
    'Copy every sheet from the workbook with this macro
    Set Sourcewb = ActiveWorkbook
    'Create new folder to save the new files in
    DateString = Format(Now, "dd-mm-yyyy hhmmss")
    foldername = CreateObject("WScript.Shell") _
                  .specialfolders("Desktop") & "\Macro\" & Sourcewb.Name & " " & DateString
    MkDir foldername
    'Copy every visible sheet to a new workbook
    For Each sh In Sourcewb.Worksheets
        'If the sheet is visible then copy it to a new workbook
        If sh.Visible = -1 Then
            sh.Copy
            'Set Destwb to the new workbook
            Set Destwb = ActiveWorkbook
            'Determine the Excel version and file extension/format
            With Destwb
                If Val(Application.Version) < 12 Then
                    'You use Excel 97-2003
                    FileExtStr = ".xls": FileFormatNum = -4143
                Else
                    'You use Excel 2007-2016
                    If Sourcewb.Name = .Name Then
                        MsgBox "Your answer is NO in the security dialog"
                        GoTo GoToNextSheet
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                End If
            End With
            'Change all cells in the worksheet to values if you want
            If Destwb.Sheets(1).ProtectContents = False Then
                With Destwb.Sheets(1).UsedRange
                    .Cells.Copy
                    .Cells.PasteSpecial xlPasteValues
                    .Cells(1).Select
                End With
                Application.CutCopyMode = False
            End If
            'Save the new workbook, email it, and close it
            Set otlNewMail = otlApp.CreateItem(olMailItem)
            DateString = "Credits " & Format(Date, "mmm yyyy") & " "
            With Destwb
                .SaveAs foldername _
                      & "\" & DateString & Destwb.Sheets(1).Name & FileExtStr, _
                        FileFormat:=FileFormatNum
            End With
            myPath = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
            mySubline = WorksheetFunction.Proper(RemoveNumbers(Replace(Replace(Replace((ActiveWorkbook.Name), (DateString), ""), ".xlsx", ""), ".", " ")))
            myFcm = CreateObject("WScript.Shell").specialfolders("Desktop") & "\xxx.pdf"
            myCredit = CreateObject("WScript.Shell").specialfolders("Desktop") & "\xxx.pdf"
            subname = WorksheetFunction.Proper(RemoveNumbers(Replace(Left((ActiveWorkbook.Name), InStr((ActiveWorkbook.Name), ".") - 1), (DateString), "")))
            MySendTo = Replace(Replace((ActiveWorkbook.Name), (DateString), ""), ".xlsx", "")
            With Destwb
                .Close False
            End With
            
 
            strbody = "email body"

 
            
            With otlNewMail
            .SentOnBehalfOfName = ""
            .GetInspector ' ## This inserts default signature
                 Signature = .HTMLBody ' ## Capture the signature HTML
                 Signature = Replace(Signature, _
            "<p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p>", _
            "<p class=MsoNormal><o:p>&nbsp;</o:p></p>")
                .Subject = mySubject & " " & mySubline
                .To = MySendTo & "@email.com"
                .CC = ""
                .Attachments.Add myPath
                .Attachments.Add myFcm
                .Attachments.Add myCredit
                .HTMLBody = strbody & Signature
                .Display
            End With

            Set otlNewMail = Nothing
        End If
GoToNextSheet:
    Next sh
    Worksheets("Macro").Visible = True
    Worksheets("Credits").Visible = True
    Worksheets("Email List").Visible = True
    MsgBox "                                     Completed." & vbCrLf & "      You can find the files in the Macro folder"
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        Sheets("Macro").Select
    End With
    End If
End Sub
Sub findemail()
    Dim FindString As String
    Dim Rng As Range
    Dim a As Long
    Application.Calculation = xlManual

    ThisWorkbook.Worksheets("Credits").Range("L1").CurrentRegion.Sort Key1:=ThisWorkbook.Worksheets("Credits").Range("L1"), _
    DataOption1:=xlSortTextAsNumbers, Header:=xlYes
    
    a = ThisWorkbook.Worksheets("Credits").Range("L2").End(xlDown).Row

    For Each cell In Sheets("Credits").Range("L2:L" & a)
        FindString = cell.Value
        
        If Trim(FindString) <> "" Then
            'The 2nd worksheet is assumed to be Sheet2. Change this if it is not the case
            With Sheets("Email List").Range("A:A")
                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not Rng Is Nothing Then
                    Application.GoTo Rng, True
                    'In Sheet 2: This line shifts 5 cells to the right and gets the email value
                    TheEmail = ActiveCell.Offset(0, 1).Value
                    'In Sheet 1: The country value is pasted into the cell 2 cells to the left of the cell containing the ID
                    cell.Offset(0, 30).Value = TheEmail
                    
                Else
                    cell.Offset(0, 30).Value = "Not Found"
                    MsgBox ("Unable to find some emails. Please check the Credits Column AP")
                End If
            End With
        End If

    Next
    Application.ScreenUpdating = True
    Sheets("Macro").Select
End Sub
0 Answers
Related