1
votes

I want an Event-handling macros that maximizes the excel workbook and window on opening. I want this as a personal macro that would work on any of my workbooks. I currently have this:

Private Sub Workbook_Open()
   Application.WindowState = xlMaximized
   ActiveWindow.WindowState = xlMaximized
End Sub

It is saved the "VBAProject (PERSONAL.XLSB)" within "ThisWorkBook"

The code maximizes the workbook, but it gives me the Run-time error '91' when it reaches "ActiveWindow.WindowState = xlMaximized". Can anyone help me with this?

thank you

1

1 Answers

1
votes

Your code is assuming that there's an ActiveWindow, which isn't guaranteed.

Use If Not [object] Is Nothing Then... to check if an object reference is usable:

Private Sub Workbook_Open()
   Application.WindowState = xlMaximized
   If Not ActiveWindow Is Nothing Then ActiveWindow.WindowState = xlMaximized
End Sub