以下代码已经可以实现打开指定位置的Excel文件,但不知如何实现打开后使其最大化、最小化功能禁用的代码,请高人补充一下,不甚感激。
Dim fso,fname
Set fso=CreateObject("scripting.FileSystemObject")
fname="D:\"&FormatDateTime(date,2)&".xls"
Dim objExcelApp
Set objExcelApp=CreateObject("Excel.Application")
objExcelApp.visible=true
objExcelApp.workbooks.open fname
----请补充打开Excel后使工作簿最大化、最小化窗口禁用的代码-----
Set objExcelApp=nothing
最佳答案
打开Excel,菜单-工具-宏-Visual Basic 编辑器
在VBAProject下插入模块,粘贴一下代码
Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000
Const GWL_STYLE = (-16)
Sub hide(hwnd As Long)
Dim lWnd As Long
lWnd = GetWindowLong(hwnd, GWL_STYLE)
lWnd = lWnd And Not (WS_MINIMIZEBOX)
lWnd = lWnd And Not (WS_MAXIMIZEBOX)
lWnd = SetWindowLong(hwnd, GWL_STYLE, lWnd)
Dim h As Long
h = FindWindowEx(hwnd, 0&, "EXCEL2", vbNullString)
h = FindWindowEx(h, 0&, "MsoCommandBar", "工作表菜单栏")
EnableWindow h, 0
End Sub
保存为xla文件,比如D:\Hide.xla
在你指定代码补充的地方插入
objExcelApp.Workbooks.Open "D:\Hide.xla"
objExcelApp.Run "hide", objExcelApp.hwnd
这样就运行了上面保存的宏
菜单和最大化最小化都被屏蔽
提问者对于答案的评价:
谢谢啦!
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc271982.html