Di chuyển Form theo file Excel

Liên hệ QC

Miccpro

Thành viên thường trực
Tham gia
9/12/10
Bài viết
236
Được thích
10
Chào các anh chị GPE!
Em sưu tầm được đoạn code để form luôn cố định ở thanh công cụ bên trái khi ta di chuyển hoặc phóng to thu nhỏ bảng tính Excel. Giờ em muốn form luôn cố định ở cạnh Thanh cuộn dọc bên phải Excel thì sửa code thế nào ạ!
Em xin cảm ơn !
Mã:
Option Explicit
#If VBA7 Then
    #If Win64 Then
      Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongLong, ByVal hWnd2 As LongLong, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongLong
    #Else
      Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    #End If
    Private Declare PtrSafe Function SetParent Lib "user32" (ByVal hWndChild As LongPtr, ByVal hWndParent As LongPtr) As Long
    Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As Any, ByVal hWnd As LongPtr) As LongPtr
#Else
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndParent As Long) As Long

    Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As Any, ByVal hwnd As Long) As Long
#End If
Private hMain As LongPtr
Private hXLD As LongPtr
Private hXL7 As LongPtr
Private meHwnd  As LongPtr
Function NewHandle() As Boolean
Dim Aw As Excel.Window
  Dim th As LongPtr
    hMain = Application.hWnd
    hXLD = FindWindowEx(hMain, 0&, "XLDESK", vbNullString)
    On Error Resume Next
    Set Aw = ActiveWindow
    On Error GoTo 0
    If Aw Is Nothing Then
      th = FindWindowEx(hXLD, 0&, "EXCEL7", vbNullString)
    Else
      th = FindWindowEx(hXLD, 0&, "EXCEL7", Aw.Caption)
      If th = 0 Then th = FindWindowEx(hXLD, 0&, "EXCEL7", Aw.Caption & "  [Read-Only]"): _
      If th = 0 Then th = FindWindowEx(hXLD, 0&, "EXCEL7", Aw.Caption & "  [Repair]"): _
      If th = 0 Then th = FindWindowEx(hXLD, 0&, "EXCEL7", Aw.Caption & "  [Repaired]")
    End If
    If th <> hXL7 Then hXL7 = th: NewHandle = True
End Function
Sub MoveFormWithExcel(frm As Object)
    IUnknown_GetWindow frm, VarPtr(meHwnd)
    If NewHandle() Then SetParent meHwnd, hXL7
End Sub
Private Sub UserForm_Initialize()
    Call MoveFormWithExcel(Me)
End Sub
 

File đính kèm

  • MoveForm.xlsm
    17.3 KB · Đọc: 11
Web KT
Back
Top Bottom