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:
Report code: (there's a lot more than 2 lol)
Sample of printed report
Sample of Saved Report
The printer code
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