ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何利用vba激活已打开的本工作簿所在文件夹窗口或未打开时打开所在文件夹?

如何利用vba激活已打开的本工作簿所在文件夹窗口或未打开时打开所在文件夹?

作者:绿色风 分类: 时间:2022-08-17 浏览:91
楼主
kevinchengcw
Q: 如何利用vba激活已打开的本工作簿所在文件夹窗口或未打开时打开所在文件夹?
A: 代码如下:
  1. '定义常量
  2. Private Const GW_HWNDNEXT = 2
  3. Private Const GW_HWNDFIRST = 0
  4. Private Const WM_GETTEXT = &HD

  5. '定义要引用的API函数
  6. Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal Hwnd As Long, ByVal wFlag As Long) As Long
  7. Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
  8. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal Hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  9. 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
  10. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  11. Private Declare Function SetForegroundWindow Lib "user32" (ByVal Hwnd As Long) As Long
  12. Private Declare Function FindWindow Lib "user32 " Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

  13. Sub test()
  14. Dim Hwnd&, Str$
  15. If ThisWorkbook.Path <> "" Then  '如果当前工作簿已保存到具体位置,则
  16.     Hwnd = FindWindow("XLMAIN", vbNullString)  '查找Excel程序窗口
  17.     Hwnd = GetWindow(Hwnd, GW_HWNDFIRST)  '查找同级窗口中的第一个程序窗口的句柄
  18.     While Hwnd <> 0  '当句柄有效时循环
  19.         Str = String(256, Chr(0))  '设置256字符的字符串变量
  20.         GetClassName Hwnd, Str, 255  '提取该句柄对应的类名到变量
  21.         Str = Replace(S, Chr(0), "")  '替换到无效的字符
  22.         If Str = "CabinetWClass" And GetUrl(Hwnd) = ThisWorkbook.Path Then  '如果浏览器窗口且浏览位置是本工作簿所在文件夹,则
  23.             SetForegroundWindow Hwnd   '将窗口激活置顶
  24.             Exit Sub  '取出程序
  25.         End If
  26.         Hwnd = GetNextWindow(Hwnd, GW_HWNDNEXT)  '继续取下一个窗口的句柄
  27.     Wend
  28.     Shell "explorer.exe """ & ThisWorkbook.Path & """",, vbMaximizedFocus  '如果未能找到已打开的窗口则新打开一个
  29. Else
  30.     MsgBox "文件还未保存!"
  31. End If
  32. End Sub

  33. Function GetUrl(Hwnd As Long) As String  '获取窗口对应地址自定义函数
  34. Dim NexthWnd&, Str$
  35. NexthWnd = 0  '初始化句柄为0(即无效)
  36. NexthWnd = FindWindowEx(Hwnd, NexthWnd, vbNullString, vbNullString)  '获取传入的窗口句柄的子窗口句柄
  37. While NexthWnd <> 0  '找到的句柄有效时执行循环
  38.     Str = String(256, Chr(0))    '设置256字符的字符串变量
  39.     GetClassName NexthWnd, Str, 255  '获取对应窗体控件的类名
  40.     Str = Replace(S, Chr(0), "")  '替换掉无意义字符
  41.     If Str = "Edit" Then  '如果是文本框,则
  42.         Str = String(256, Chr(0))   '设置256字符的字符串变量
  43.         SendMessage NexthWnd, WM_GETTEXT, 255, Str  '获取窗口文本
  44.         Str = Replace(S, Chr(0), "")  '替换掉无意义字符
  45.         GetUrl = Str  '返回得到的文本
  46.         If Len(Str) > 0 Then Exit Function  '如果得到的是非空字符串则退出函数
  47.     Else  '否则
  48.         GetUrl = GetUrl(NexthWnd)  '调取下一窗体地址
  49.         If Len(GetUrl) > 0 Then Exit Function   '如果得到的是非空字符串则退出函数
  50.     End If
  51.     NexthWnd = FindWindowEx(Hwnd, NexthWnd, vbNullString, vbNullString)  '循环到下一窗体
  52. Wend
  53. End Function
附示例文件.


Demo.rar
2楼
chenlifeng
反馈K哥哥:
    您上面的代码写得非常的出色!我之前在询问时,就已经被您的用心和思虑周详给完全打动了!
后来,我在运用到我的WORD代码时,只用到了下面的代码,就解决了问题,谢谢您的启发!
  1. Private Sub Document_Close()
  2. Dim Doc$
  3. Doc = ActiveDocument.Path
  4.      Shell Environ("comspec") & " /c compact /c/i """ & Doc & """", vbHide
  5.      Shell "explorer.exe /select,""" & ActiveDocument.FullName & """", vbMaximizedFocus
  6.      ActiveDocument.Save
  7. End Sub
3楼
biaotiger1
API函数的应用之一,标记下。

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap