Error With VBA Trying To Centre A Window

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code below that modifies a workbook to eliminate tabs, ribbons, formula bars, scrollbars and what not). It resides in wb1 but is applied to wb2 (so it's not the calling workbook that get modified, it's wb2). Everything seems to be working well, but I am unable to centre the resized window on my monitor with the code I found and applied.

Rich (BB code):
Sub ReadingView()

    Dim maxWidth As Integer
    Dim maxHeight As Integer
    Dim appLeft As Integer
    Dim appTop As Integer

    With Application
        With Workbooks("permit_info.xlsm")
            Application.windowstate = xlMaximized
            maxWidth = Application.Width
            maxHeight = Application.Height
    ' store current ActiveWorkbook settings
            View.drawingobjects = .DisplayDrawingObjects
    ' set
            Windows("permit_info.xlsm").Visible = True
            Workbooks("permit_info.xlsm").Worksheets("FORM").Activate
    End With

    With ActiveWindow
 ' store current ActiveWindow settings
        View.headings = .DisplayHeadings
        View.gridlines = .DisplayGridlines
        View.hscrollbar = .DisplayHorizontalScrollBar
        View.vscrollbar = .DisplayVerticalScrollBar
        View.wkbtabs = .DisplayWorkbookTabs

  ' set
        .DisplayHeadings = False
        .DisplayHorizontalScrollBar = False
        .DisplayVerticalScrollBar = False
        .DisplayWorkbookTabs = False
        .DisplayGridlines = False

        appLeft = maxWidth / 2
        appTop = maxHeight / 2
       
        .Width = 480
        .Height = 250
        .Top = appTop
        .Left = appLeft
     End With
 
 ' set
    .ExecuteExcel4Macro _
        "SHOW.TOOLBAR(""Ribbon"",False)"
 
 ' store current Application Settings
    View.formulabar = .DisplayFormulaBar
    View.statusbar = .DisplayStatusBar
 'set
    .DisplayFormulaBar = False
    .DisplayStatusBar = False
 
    End With
End Sub

The window size is static at 480px x 250px, but the top and left values are variable I hope based on the size of the monitor. I don't want to make the changes at the application level because I wish to keep wb1 maximized behind wb2 (in its resized and centred state.)

I am receiving an error : "Unable to set the width property of the Window class". with the line in red.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You are getting the error since you're trying to resize a maximized window. Try this:
VBA Code:
    appTop = maxHeight / 2

    .WindowState = xlNormal  ' <<<<<
    .Width = 480
    .Height = 250
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

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