In Visual Studio 2017 I'm creating a Windows Form Application with a Form1
where the user picks an opened Excel workbook to listen to its BeforeSave
event.
Once that workbook is saved, it gets detected by the program which then colects some specific data from the sheets and opens Form2
with that data.
The issue I'm having is that when I load Form2
on the BeforeSave
event, its controls (like Labels, buttons, textboxes, etc) don't show correctly. They appear like boxes with a background color.
Here's my code reduced and changed to the part that matters:
' Reference:
' Microsoft Excel 15.0 Object Library
Imports Microsoft.Office.Interop
Public Class Form1
Private xlApp As Excel.Application
Public Shared WithEvents xlBook As Excel.Workbook
Private Shared Sub Workbook_BeforeSave(SaveAsUI As Boolean, ByRef Cancel As Boolean) Handles xlBook.BeforeSave
Form2.Show()
'Form1.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
xlApp = Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
xlBook = xlApp.Workbooks(1)
Catch
MsgBox("Excel isn't opened.")
Close()
End Try
End Sub
End Class
Edit: Using Application.Run(Form2)
works (don't know why) but then I also can't close Form1
with Form1.Close()
.