Hi,
I've spent a lot of time on these forums, I've found answers to many questions here and saved so much time, thank you!
But I have what seems to be an extremely unusual problem which I need some help with.
I have two data extracts which are dumped out of another program, in a csv format, these files are downloaded via internet explorer and automatically opened into excel. These files have a different name each time they are generated.
I've written some code in a separate workbook which executes each time any workbook is opened, it determines whether the opened workbook is either of the data extracts which I need to manipulate, and automatically runs the appropriate sub to manipulate that extract type.
It all works perfectly using example csv files stored on my computer, however when the csv's are downloaded and opened via internet explorer it appears to run the code twice, and leaves behind an empty internet explorer pane. (Which it doesn't do when I have the automatic sub disabled)
Macro workbook:
ThisWorkbook
Class Module (CExcelEvents)
Module 1 (I've removed the other subs, else this would have been an extremely large post!)
Thanks for any help you can provide!
I've spent a lot of time on these forums, I've found answers to many questions here and saved so much time, thank you!
But I have what seems to be an extremely unusual problem which I need some help with.
I have two data extracts which are dumped out of another program, in a csv format, these files are downloaded via internet explorer and automatically opened into excel. These files have a different name each time they are generated.
I've written some code in a separate workbook which executes each time any workbook is opened, it determines whether the opened workbook is either of the data extracts which I need to manipulate, and automatically runs the appropriate sub to manipulate that extract type.
It all works perfectly using example csv files stored on my computer, however when the csv's are downloaded and opened via internet explorer it appears to run the code twice, and leaves behind an empty internet explorer pane. (Which it doesn't do when I have the automatic sub disabled)
Macro workbook:
ThisWorkbook
Code:
Private ExcelEvents As CExcelEvents
Private Sub Workbook_Open()
Set ExcelEvents = New CExcelEvents
End Sub
Class Module (CExcelEvents)
Code:
Private WithEvents XLApp As Application
Private Sub Class_Initialize()
Set XLApp = Application
End Sub
Private Sub XLApp_WorkbookOpen(ByVal WB As Workbook)
Call AutoGenerateReport
End Sub
Module 1 (I've removed the other subs, else this would have been an extremely large post!)
Code:
Public CurrentFileVersion As String
Public UpdatePath As String
Public MacroName As String
Public AutoRun As Boolean
Public OpenedBook As Workbook
Public LastMacro As String
Public WB As Workbook
Public WS As Worksheet
Sub AutoGenerateReport()
Application.ScreenUpdating = False
Dim OpenedBookN As String
Dim NotRan As Boolean
Set OpenedBook = ActiveWorkbook
OpenedBookN = OpenedBook.Name
If OpenedBook.Name = ThisWorkbook.Name Then
Exit Sub
' The above checks to see if the opened workbook was itself, and if so, exits the sub.
End If
Application.ThisWorkbook.Activate
Sheets("Config").Select
If Range("S12").Text = "Yes" Then
ToggleAutoGenerate
' disables the automatic code from launching if another book is opened while the macro is running (some of my other macros write a log into another book)
AutoRun = True
' this disables certain parts of PickMacro/PackMacro (these only need to be disabled if these macros are called from this one)
Application.DisplayAlerts = False
PickMacro
PackMacro
ToggleAutoGenerate
're-enables the code to launch upon workbook start
Application.DisplayAlerts = True
' The following code checks to see if the workbook which was opened (which prompted the code to run) is still open; if it is, neither PickMacro or PackMacro found any data to manipulate
For Each WB In Application.Workbooks
If WB.Name = OpenedBookN Then
OpenedBook.Activate
NotRan = True
Exit For
End If
Next
If NotRan = False Then
'if this is false, either the PickMacro or PackMacro ran, and closed the workbook which was opened
If LastMacro = "pick" Then
Sheets("PickReport").Select
Else
If LastMacro = "pack" Then
Sheets("PackReport").Select
Else
MsgBox "Macro ran, but couldn't find report"
End If
End If
Else
End If
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Thanks for any help you can provide!