Automaticly run macro when opening CSV from internet

Kotoa

New Member
Joined
Feb 15, 2014
Messages
2
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
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!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

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