Note that there are some explanatory texts on larger screens.

plurals
  1. POCapturing Popups, Alerts, and Exceptions from Excel with VB 2010
    primarykey
    data
    text
    <p>This is my first time posting in stackoverflow. I started learning VB 2010 yesterday (though I do know some Ruby) and have googled this problem a lot but haven't found much. </p> <p>I've written a program that takes a CSV list of filepaths (all .XLS files) and attempts to open them. These excel workbooks were flagged as unreadable by another program; my program tries to capture the warning, popup, or exception that caused the error. It works for most exceptions (password protected, Unreadable Content, Dangerous File, etc.), but some popups that require you to click the 'OK' button to continue aren't caught. Maybe they aren't categorized as Exceptions or something, I'd love to know. It would be great to at least get the text form of them. Here is my script:</p> <pre><code>Private Sub runReportButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles runReportButton.Click Dim Exl As New Excel.Application() Exl.Visible = False Exl.DisplayAlerts = False For Each row As DataGridViewRow In dgvReport.Rows If Not IsNothing(row.Cells(0).Value) Then If row.Cells(0).Value.ToString.Contains(".xls") Then Try 'Open the workbook and attempt to catch the error!' 'Use bogus passwords to cause Password Protected Error, Readonly disabled so the Password popup shows. Update links disabled because it wasn't being caught anyways' Dim wb As Excel.Workbook = Exl.Workbooks.Open(row.Cells(0).Value.ToString, [ReadOnly]:=False, [WriteResPassword]:="WWWWWWWWWWW", [Password]:="WWWXWXWWWXW", [UpdateLinks]:=False) row.Cells(4).Value = "Could not catch an error :(" wb.Close(0) dgvReport.Refresh() Catch ex As Exception 'Writes the Exception Message to the data grid view' row.Cells(4).Value = ex.Message End Try ProgressBar1.Value += 1 End If End If Exl.Quit() Next row dgvReport.Refresh() End Sub </code></pre> <p>The reason I'm having a problem is that certain popups only occur when I open the file manually. Even with Excel set to visible and display alerts on, programmatically the file opens and closes without any popups. One particular warning is the following:</p> <blockquote> <p>"One or more worksheets in this workbook have names that contain square brackets:[]. To avoid unexpected results when referencing these worksheets, remove any square brackets from their names."</p> </blockquote> <p>It doesn't seem like a fatal error, but it causes the other program to flag it as unreadable because we cannot be certain of the integrity of the contents (i.e. the unexpected results). So my need isn't to ignore these popups or improve the other program, it's to capture this pesky popup message. If anyone can shed any light on the duality of this popup's behavior or provide a different approach to capturing alerts it would be much appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload