VBA Code Corrupting files suddenly

rachelm920

New Member
Joined
Jan 26, 2012
Messages
10
Hi all,
I have a code that opens all Excel files in the designated folder, then performs actions based on the file name. I've been using this method for a long time, but suddenly recently its been corrupting files. Then the whole thing just sits there with an error message because it can't open the file. The file corruption happens randomly and on different file names each time, so it's not them. Some of the code has adding tables and formatting, but it hiccups on any of them, and usually the more simple ones.

Sample code:
Main Code:
VBA Code:
Sub AllFiles()
Dim folderpath, filename as string
Dim wb as workbook
folderpath = "C:\Samplefolder\Reports\"
filename = Dir(folderpath & "*.xlsx")
Do while filename <> ""
 Set wb = Workbooks.Open(folderPath & filename)
         Application.DisplayAlerts = False
        'Call a subroutine here to operate on the just-opened workbook
        Reports
            
        filename = Dir
    Loop
  Application.ScreenUpdating = False
  Application.Wait (Now + TimeValue("0:00:30"))
End Sub

Report code: (there's a lot more than 2 lol)
VBA Code:
Sub Reports()
'The report names'
Dim Report1, Report2 as Integer
Dim MyFile As string
'Turn off the alerts'
Application.DisplayAlerts = False

'Define the reports'
Report1 = Instr(MyFile, "Report1")
Report2 = Instr(Myfile, "Report2")

'Coding for the reports'
    If Report1 = 1 then
    Call MyReport1
    ElseIf Report2 = 1 then
    Call MyReport2
End If
End Sub

Sample of printed report
VBA Code:
Sub MyReport1()
Dim MyName, MyWorkbook, MyFolder As String
    MyName = ActiveWorkbook.FullName
    MyWorkbook = MyName
    MyFolder = "C:\SampleFolder\Reports\MyReport1\"
        Call PrintOffice
            ActiveWorkbook.SaveAs MyFolder & ActiveWorkbook.Name
                Kill MyWorkbook
                ActiveWorkbook.Close
                    
    MyName = ""
    MyWorkbook = ""
    MyFolder = ""

        
End Sub

Sample of Saved Report
VBA Code:
Sub MyReport2()
Dim MyName, MyWorkbook, MyFolder As String
    MyName = ActiveWorkbook.FullName
    MyWorkbook = MyName
    MyFolder = "C:\SampleFolder\Reports\MyReport2\"
        ActiveWorkbook.SaveAs MyFolder & ActiveWorkbook.Name
            Kill MyWorkbook
            ActiveWorkbook.Close
            
    MyName = ""
    MyWorkbook = ""
    MyFolder = ""
    
End Sub

The printer code
VBA Code:
Sub PrintOffice()
Application.ActivePrinter = "\\domainname\printername on Ne04:"
    

ActiveWindow.SelectedSheets.PrintOut copies:=1, collate:=True, ignoreprintareas:=False
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Your SaveAs is not specifying a file format. You should always include that.
 
Upvote 0
OK I changed it from
VBA Code:
ActiveWorkbook.SaveAs MyFolder & ActiveWorkbook.Name
To
VBA Code:
ActiveWorkbook.SaveAs MyFolder & ActiveWorkbook.Name &".xlsx"

but now it's adding .xlsx to the file name. Is there something else I'm missing?
 
Upvote 0
Given that they are existing workbooks, I'd just use whatever file format they are already:

VBA Code:
ActiveWorkbook.SaveAs MyFolder & ActiveWorkbook.Name, activeworkbook.fileformat
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,408
Members
452,325
Latest member
BlahQz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top