How to hide workbook behind userform?

前提是你 提交于 2020-01-25 10:05:02

问题


I've tried multiple methods to hide specific workbook behind userform!

Last code I've used is here:

Private Sub UserForm_Layout()
  Application.Left = MainWindow.Left
  Application.Top = MainWindow.Top
End Sub
Private Sub UserForm_Activate()
  Application.Left = Me.Left
  Application.Top = Me.Top
  Application.Width = Me.Width * 0.85
  Application.Height = Me.Height * 0.85
End sub

It will hide application window behind userform, but if there is multiple workbooks open and I activate one of them, when I click on userform afterwards, it will move only active workbook within userform!

How to instruct to always affect only specific workbook with this function?

Also, by jumping from one UF to another same code will be executed each time!

Basically, I need to have specific workbook hidden behind userform ALWAYS and not accessible by users, but all other already opened workbooks or workbooks I intend to open must not be affected by this! Other workbooks must be accessible, and visible and shouldn't dissappear, or move if I use this or similar function!

I also tried application.visible = false but, it is dangerous as it also affects other workbooks and application is OFC not visible on taskbar, and any error may cause application to left open in background and not visible by user!

If you suggest any other method to achieve above mentioned requirement I would be happy to try it!

Thnx


回答1:


Try hiding the form's parent window

Private Sub UserForm_Initialize()
    ThisWorkbook.Windows(1).Visible = False
End Sub

Private Sub UserForm_Terminate()
    ThisWorkbook.Windows(1).Visible = True
End Sub

Or determine screen coordinates of the form and apply them the parent

Private Sub UserForm_Initialize()
    With ThisWorkbook.Windows(1)
        .WindowState = xlNormal
        .Left = Me.Left + Application.Left  'Calculate exact Screen.Left coordinate
        .Top = Me.Top + Application.Top     'Calculate exact Screen.Top coordinate
        .Width = Me.Width * 0.85
        .Height = Me.Height * 0.85
    End With
End Sub

.

To get screen resolution use GetSystemMetrics function:


#If VBA7 Then
    Declare PtrSafe Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" _
    (ByVal nIndex As Long) As Long
#Else
    Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" _
    (ByVal nIndex As Long) As Long
#End If
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1



Private Sub setMonitors()

    celTotalMonitors = GetSystemMetrics32(80)

End Sub



Private Sub setResolution()

    'The width of the virtual screen, in pixels
    celScreenResolutionX = Format(GetSystemMetrics32(78), "#,##0")

    'The height of the virtual screen, in pixels
    celScreenResolutionY = Format(GetSystemMetrics32(79), "#,##0")

    'celScreenResolutionY = celScreenResolutionY.Value \ celTotalMonitors
End Sub


来源:https://stackoverflow.com/questions/49950271/how-to-hide-workbook-behind-userform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!