Office 2007/2010を使用していて、ユーザーにリボンから操作させたくない、リボン領域が邪魔で何とかしたい、と思っている方も多いと思います。今回はリボンを非表示、もしくは最小化する方法をまとめてみます。
1. Custom UI Editor ToolでOffice ファイルを開きます。
2. 下記コードを貼り付け、上書き保存します。
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="true" />
</customUI>
1. Access ファイルを開きます。
2. 「USysRibbons」テーブルの「RibbonXml」フィールドに下記コードを貼り付け、上書き保存します。
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="true" />
</customUI>
1. Excel ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Sub HideRibbon()
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
End Sub
1. Accessで新規データベースを作成します。
2. フォーム上にボタンを配置し、クリック時のイベントに下記コードを貼り付けます。
DoCmd.ShowToolbar "Ribbon", acToolbarNo
1. Office ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Sub MinimizeRibbon()
SendKeys "^{F1}"
End Sub
1. Office ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Private Type INPUT_TYPE
dwType As Long
xi(0 To 23) As Byte
End Type
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Declare Function SendInput Lib "user32" (ByVal nInputs As Long, pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const INPUT_KEYBOARD = 1
Private Const KEYEVENTF_KEYDOWN = &H0
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_CONTROL = &H11
Private Const VK_F1 = &H70
Sub MinimizeRibbonAPI()
Dim ki As KEYBDINPUT
Dim ip(3) As INPUT_TYPE
'Ctrlキーを押す
ki.wVk = VK_CONTROL
ki.dwFlags = KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYDOWN
ip(0).dwType = INPUT_KEYBOARD
Call CopyMemory(ip(0).xi(0), ki, Len(ki))
'F1キーを押す
ki.wVk = VK_F1
ki.dwFlags = KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYDOWN
ip(1).dwType = INPUT_KEYBOARD
Call CopyMemory(ip(1).xi(0), ki, Len(ki))
'Ctrlキーを離す
ki.wVk = VK_CONTROL
ki.dwFlags = KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP
ip(2).dwType = INPUT_KEYBOARD
Call CopyMemory(ip(2).xi(0), ki, Len(ki))
'F1キーを離す
ki.wVk = VK_F1
ki.dwFlags = KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP
ip(3).dwType = INPUT_KEYBOARD
Call CopyMemory(ip(3).xi(0), ki, Len(ki))
Call SendInput(4, ip(0), Len(ip(0)))
End Sub
1. Word ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Sub MinimizeRibbonWord()
Application.ActiveWindow.ToggleRibbon
End Sub
1. Word ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Sub FullScreenWord()
ActiveWindow.View.FullScreen = Not ActiveWindow.View.FullScreen
End Sub
1. Excel ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Sub FullScreenExcel()
Application.DisplayFullScreen = Not Application.DisplayFullScreen
End Sub
1. Office ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Option Explicit
Private Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function InvalidateRect Lib "user32" (ByVal hWnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function UpdateWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Const CHILDID_SELF = 0&
Private Const ROLE_SYSTEM_PUSHBUTTON = &H2B
Private Const ROLE_SYSTEM_MENUITEM = &HC
Private Const WM_SETREDRAW = &HB
Public Sub ToggleRibbon(myFlag As Boolean)
'myFlag(True:リボンの最小化 , False:リボンの最小化解除)
Dim accBtn As Office.IAccessible
Dim accMenu As Office.IAccessible
Dim hDesktop As Long
On Error GoTo myErr
hDesktop = GetDesktopWindow()
If hDesktop = 0& Then Exit Sub
'再描画禁止
Call SendMessage(hDesktop, WM_SETREDRAW, 0&, 0&)
Set accBtn = Application.CommandBars("Ribbon")
Set accBtn = GetAcc(accBtn, "ツール バーのカスタマイズ", ROLE_SYSTEM_PUSHBUTTON)
accBtn.accDoDefaultAction (CHILDID_SELF)
DoEvents
Set accMenu = GetAcc(accBtn, "リボンの最小化", ROLE_SYSTEM_MENUITEM)
Select Case accMenu.accState(CHILDID_SELF)
Case &H100000 'チェックなし
If myFlag = True Then
accMenu.accDoDefaultAction (CHILDID_SELF)
Else
accBtn.accDoDefaultAction (CHILDID_SELF)
End If
Case &H100010 'チェックあり
If myFlag = True Then
accBtn.accDoDefaultAction (CHILDID_SELF)
Else
accMenu.accDoDefaultAction (CHILDID_SELF)
End If
End Select
'再描画許可
Call SendMessage(hDesktop, WM_SETREDRAW, 1&, 0&)
Call InvalidateRect(0&, 0&, True)
Call UpdateWindow(hDesktop)
If Not accMenu Is Nothing Then Set accMenu = Nothing
If Not accBtn Is Nothing Then Set accBtn = Nothing
Exit Sub
myErr:
Debug.Print "実行時エラー:" & Err.Number & " , " & Err.Description
Call SendMessage(hDesktop, WM_SETREDRAW, 1&, 0&)
Call InvalidateRect(0&, 0&, True)
Call UpdateWindow(hDesktop)
If Not accMenu Is Nothing Then Set accMenu = Nothing
If Not accBtn Is Nothing Then Set accBtn = Nothing
End Sub
Private Function GetAcc(myAcc As Office.IAccessible, myAccName As String, myAccRole As Long) As Office.IAccessible
Dim ReturnAcc As Office.IAccessible
Dim ChildAcc As Office.IAccessible
Dim List() As Variant
Dim Count As Long
Dim i As Long
If (myAcc.accState(CHILDID_SELF) <> 32769) And _
(myAcc.accName(CHILDID_SELF) = myAccName) And _
(myAcc.accRole(CHILDID_SELF) = myAccRole) Then
Set ReturnAcc = myAcc
Else
Count = myAcc.accChildCount
If Count > 0& Then
ReDim List(Count - 1&)
If AccessibleChildren(myAcc, 0&, ByVal Count, List(0), Count) = 0& Then
For i = LBound(List) To UBound(List)
If TypeOf List(i) Is Office.IAccessible Then
Set ChildAcc = List(i)
Set ReturnAcc = GetAcc(ChildAcc, myAccName, myAccRole)
If Not ReturnAcc Is Nothing Then Exit For
End If
Next
End If
End If
End If
Set GetAcc = ReturnAcc
End Function
Public Sub Test()
'リボンの最小化(Falseで最小化解除)
Call ToggleRibbon(True)
End Sub
1. Office ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Option Explicit
Private Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function WindowFromAccessibleObject Lib "oleacc" (ByVal pacc As Office.IAccessible, ByRef phwnd As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Const CHILDID_SELF = 0&
Private Const MK_LBUTTON = &H1
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
'accRole
Private Const ROLE_SYSTEM_PAGETAB = &H25
Private Const ROLE_SYSTEM_PAGETABLIST = &H3C
Private Const ROLE_SYSTEM_PROPERTYPAGE = &H26
Public Sub ToggleRibbon(myFlag As Boolean)
'myFlag(True:リボンの最小化 , False:リボンの最小化解除)
Dim accHomeTab As Office.IAccessible
Dim accHomePage As Office.IAccessible
Dim lpxLeft As Long
Dim lpyTop As Long
Dim lpcxWidth As Long
Dim lpcyHeight As Long
Dim lParam As Long
Dim laccHomePageState As Long
Dim hNetUIHWND As Long
Dim pa As POINTAPI
On Error GoTo myErr
laccHomePageState = 0& '初期化
Set accHomeTab = Application.CommandBars("Ribbon")
Set accHomeTab = GetAcc(accHomeTab, "リボン タブ", ROLE_SYSTEM_PAGETABLIST)
Set accHomeTab = GetAcc(accHomeTab, "ホーム", ROLE_SYSTEM_PAGETAB)
If accHomeTab Is Nothing Then Exit Sub
Call WindowFromAccessibleObject(accHomeTab, hNetUIHWND) 'ハンドル取得
If hNetUIHWND = 0& Then Exit Sub
accHomeTab.accDoDefaultAction (CHILDID_SELF) 'ホームタブ選択
DoEvents 'IAccessibleアクセス待ち
'accLocation取得
Call accHomeTab.accLocation(lpxLeft, lpyTop, lpcxWidth, lpcyHeight, CHILDID_SELF)
'プロパティページの状態取得
Set accHomePage = GetAcc(accHomeTab, "ホーム", ROLE_SYSTEM_PROPERTYPAGE)
If accHomePage Is Nothing Then
'初期状態で最小化されていない場合
laccHomePageState = 0&
Else
'初期状態で最小化されている場合
laccHomePageState = 1&
End If
'クリック送信する座標設定
pa.x = lpxLeft + (lpcxWidth / 2)
pa.y = lpyTop + (lpcyHeight / 2)
Call ScreenToClient(hNetUIHWND, pa)
lParam = (pa.y * &H10000) + pa.x
If ((laccHomePageState = 0&) And (myFlag = True)) Or _
((laccHomePageState = 1&) And (myFlag = False)) Then
Call PostMessage(hNetUIHWND, WM_LBUTTONDOWN, MK_LBUTTON, lParam)
Call PostMessage(hNetUIHWND, WM_LBUTTONUP, 0&, lParam)
Call PostMessage(hNetUIHWND, WM_LBUTTONDOWN, MK_LBUTTON, lParam)
Call PostMessage(hNetUIHWND, WM_LBUTTONUP, 0&, lParam)
End If
If Not accHomeTab Is Nothing Then Set accHomeTab = Nothing
If Not accHomePage Is Nothing Then Set accHomePage = Nothing
Exit Sub
myErr:
MsgBox "実行時エラー:" & Err.Number & vbCrLf & Err.Description, vbCritical, "処理が失敗しました。"
If Not accHomeTab Is Nothing Then Set accHomeTab = Nothing
If Not accHomePage Is Nothing Then Set accHomePage = Nothing
End Sub
Private Function GetAcc(myAcc As Office.IAccessible, myAccName As String, myAccRole As Long) As Office.IAccessible
Dim ReturnAcc As Office.IAccessible
Dim ChildAcc As Office.IAccessible
Dim List() As Variant
Dim Count As Long
Dim i As Long
If (myAcc.accState(CHILDID_SELF) <> 32769) And _
(myAcc.accName(CHILDID_SELF) = myAccName) And _
(myAcc.accRole(CHILDID_SELF) = myAccRole) Then
Set ReturnAcc = myAcc
Else
Count = myAcc.accChildCount
If Count > 0& Then
ReDim List(Count - 1&)
If AccessibleChildren(myAcc, 0&, ByVal Count, List(0), Count) = 0& Then
For i = LBound(List) To UBound(List)
If TypeOf List(i) Is Office.IAccessible Then
Set ChildAcc = List(i)
Set ReturnAcc = GetAcc(ChildAcc, myAccName, myAccRole)
If Not ReturnAcc Is Nothing Then Exit For
End If
Next
End If
End If
End If
Set GetAcc = ReturnAcc
End Function
Public Sub Test()
'リボンの最小化(Falseで最小化解除)
Call ToggleRibbon(True)
End Sub
1. Office ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Option Explicit
Private Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Private Const CHILDID_SELF = 0&
Private Const ROLE_SYSTEM_PUSHBUTTON = &H2B
Public Sub ToggleRibbon(myFlag As Boolean)
'myFlag(True:リボンの最小化 , False:リボンの最小化解除)
Dim acc As Office.IAccessible
Set acc = Application.CommandBars("Ribbon")
Set acc = GetAcc(acc, "リボンの最小化", ROLE_SYSTEM_PUSHBUTTON)
If acc Is Nothing Then Exit Sub
Select Case myFlag
Case True
If acc.accState(CHILDID_SELF) = &H100000 Then Call acc.accDoDefaultAction(CHILDID_SELF)
Case False
If acc.accState(CHILDID_SELF) = &H100008 Then Call acc.accDoDefaultAction(CHILDID_SELF)
End Select
Set acc = Nothing
End Sub
Private Function GetAcc(myAcc As Office.IAccessible, myAccName As String, myAccRole As Long) As Office.IAccessible
Dim ReturnAcc As Office.IAccessible
Dim ChildAcc As Office.IAccessible
Dim List() As Variant
Dim Count As Long
Dim i As Long
If (myAcc.accState(CHILDID_SELF) <> 32769) And _
(myAcc.accName(CHILDID_SELF) = myAccName) And _
(myAcc.accRole(CHILDID_SELF) = myAccRole) Then
Set ReturnAcc = myAcc
Else
Count = myAcc.accChildCount
If Count > 0& Then
ReDim List(Count - 1&)
If AccessibleChildren(myAcc, 0&, ByVal Count, List(0), Count) = 0& Then
For i = LBound(List) To UBound(List)
If TypeOf List(i) Is Office.IAccessible Then
Set ChildAcc = List(i)
Set ReturnAcc = GetAcc(ChildAcc, myAccName, myAccRole)
If Not ReturnAcc Is Nothing Then Exit For
End If
Next
End If
End If
End If
Set GetAcc = ReturnAcc
End Function
Public Sub Test()
'リボンの最小化(Falseで最小化解除)
Call ToggleRibbon(True)
End Sub
1. Office ファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Public Sub ToggleRibbon()
Call Application.CommandBars.ExecuteMso("MinimizeRibbon")
End Sub
以上、リボンを非表示にする方法や最小化する方法をまとめてみました。
Sponsored Links