找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 668|回复: 5

[VBA程序]:请问我VBA中可以用API函数吗?

[复制链接]
发表于 2003-3-19 11:40:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
比如我想在VBA中列出所有处于打开状态的窗口的句柄,那我该怎么做呢?谢谢指教
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 181个

财富等级: 日进斗金

发表于 2003-3-20 19:30:16 | 显示全部楼层
是指VBA的窗体(UserForm)还是指窗口(Window,包括Form,Control等),如果是窗体的话,那么就无须使用API函数,因为VBA中的窗体只在运行时加载,结束时就卸载了。关于窗口的具体操作可以参照相关资料。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2003-3-23 17:01:07 | 显示全部楼层
在VBA中怎么获得打开的Excel、Word的句柄?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 181个

财富等级: 日进斗金

发表于 2003-3-23 18:34:13 | 显示全部楼层
要想控制Word或者Excel,可以直接使用ActiveX,如:CreateObject("Excel.Application")或者CreateObject("Word.Application")来创建Word或者Excel的应用程序级对象,然后再分别使用它们提供的接口来进行控制。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2003-3-23 20:47:42 | 显示全部楼层
我是想在VBA中找出已经打开了多少个Word或Excel应用程序,如果打开的过多就关闭几个。而不是要在VBA中打开应用程序。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 181个

财富等级: 日进斗金

发表于 2003-3-23 22:54:13 | 显示全部楼层
用EnumWindows孙数来枚举顶层窗口,然后用CloseWindow或者SendMessage之类的发送消息来关闭应用程序。

  1.   [FONT=courier new]
  2. 'Example Name:EnumWindows and EnumChildWindows Callbacks  

  3. '------------------------------------------------------------------------------
  4. '
  5. ' BAS Moduel Code
  6. '
  7. '------------------------------------------------------------------------------
  8. Option Explicit

  9. Private Const LVIF_INDENT As Long = &H10
  10. Private Const LVIF_TEXT As Long = &H1
  11. Private Const LVM_FIRST As Long = &H1000
  12. Private Const LVM_SETITEM As Long = (LVM_FIRST + 6)

  13. Private Type LVITEM
  14.    mask As Long
  15.    iItem As Long
  16.    iSubItem As Long
  17.    state As Long
  18.    stateMask As Long
  19.    pszText As String
  20.    cchTextMax As Long
  21.    iImage As Long
  22.    lParam As Long
  23.    iIndent As Long
  24. End Type

  25. Public Declare Function EnumWindows Lib "user32" _
  26.   (ByVal lpEnumFunc As Long, _
  27.    ByVal lParam As Long) As Long
  28.    
  29. Public Declare Function EnumChildWindows Lib "user32" _
  30.   (ByVal hWndParent As Long, _
  31.    ByVal lpEnumFunc As Long, _
  32.    ByVal lParam As Long) As Long

  33. Private Declare Function GetWindowTextLength Lib "user32" _
  34.     Alias "GetWindowTextLengthA" _
  35.    (ByVal hwnd As Long) As Long
  36.    
  37. Private Declare Function GetWindowText Lib "user32" _
  38.     Alias "GetWindowTextA" _
  39.    (ByVal hwnd As Long, _
  40.     ByVal lpString As String, _
  41.     ByVal cch As Long) As Long
  42.    
  43. Private Declare Function GetClassName Lib "user32" _
  44.     Alias "GetClassNameA" _
  45.    (ByVal hwnd As Long, _
  46.     ByVal lpClassName As String, _
  47.     ByVal nMaxCount As Long) As Long

  48. Private Declare Function IsWindowVisible Lib "user32" _
  49.    (ByVal hwnd As Long) As Long
  50.    
  51. Private Declare Function GetParent Lib "user32" _
  52.    (ByVal hwnd As Long) As Long

  53. Private Declare Function SendMessage Lib "user32" _
  54.    Alias "SendMessageA" _
  55.   (ByVal hwnd As Long, _
  56.    ByVal wMsg As Long, _
  57.    ByVal wParam As Long, _
  58.    lParam As Any) As Long


  59. Public Function EnumWindowProc(ByVal hwnd As Long, _
  60.                                ByVal lParam As Long) As Long
  61.    
  62.   'working vars
  63.    Dim nSize As Long
  64.    Dim sTitle As String
  65.    Dim sClass As String
  66.    
  67.    Dim sIDType As String
  68.    Dim itmX As ListItem
  69.    Dim nodX As Node
  70.    
  71.   'eliminate windows that are not top-level.
  72.    If GetParent(hwnd) = 0& And _
  73.       IsWindowVisible(hwnd) Then
  74.       
  75.      'get the window title / class name
  76.       sTitle = GetWindowIdentification(hwnd, sIDType, sClass)

  77.      'add to the listview
  78.       Set itmX = Form1.ListView1.ListItems.Add(Text:=sTitle, Key:=CStr(hwnd) & "h")
  79.       itmX.SmallIcon = Form1.ImageList1.ListImages("parent").Key
  80.       itmX.SubItems(1) = CStr(hwnd)
  81.       itmX.SubItems(2) = sIDType
  82.       itmX.SubItems(3) = sClass
  83.       
  84.    End If
  85.    
  86.   'To continue enumeration, return True
  87.   'To stop enumeration return False (0).
  88.   'When 1 is returned, enumeration continues
  89.   'until there are no more windows left.
  90.    EnumWindowProc = 1
  91.    
  92. End Function


  93. Private Function GetWindowIdentification(ByVal hwnd As Long, _
  94.                                          sIDType As String, _
  95.                                          sClass As String) As String

  96.    Dim nSize As Long
  97.    Dim sTitle As String

  98.   'get the size of the string required
  99.   'to hold the window title
  100.    nSize = GetWindowTextLength(hwnd)
  101.    
  102.   'if the return is 0, there is no title
  103.    If nSize > 0 Then
  104.    
  105.       sTitle = Space$(nSize + 1)
  106.       Call GetWindowText(hwnd, sTitle, nSize + 1)
  107.       sIDType = "title"
  108.       
  109.       sClass = Space$(64)
  110.       Call GetClassName(hwnd, sClass, 64)
  111.    
  112.    Else
  113.    
  114.      'no title, so get the class name instead
  115.       sTitle = Space$(64)
  116.       Call GetClassName(hwnd, sTitle, 64)
  117.       sClass = sTitle
  118.       sIDType = "class"
  119.    
  120.    End If
  121.    
  122.    GetWindowIdentification = TrimNull(sTitle)

  123. End Function


  124. Public Function EnumChildProc(ByVal hwnd As Long, _
  125.                               ByVal lParam As Long) As Long
  126.    
  127.   'working vars
  128.    Dim sTitle As String
  129.    Dim sClass As String
  130.    Dim sIDType As String
  131.    Dim itmX As ListItem

  132.   'get the window title / class name
  133.    sTitle = GetWindowIdentification(hwnd, sIDType, sClass)

  134.   'add to the listview
  135.    Set itmX = Form2.ListView1.ListItems.Add(Text:=sTitle)
  136.    itmX.SmallIcon = Form2.ImageList1.ListImages("child").Key
  137.    itmX.SubItems(1) = CStr(hwnd)
  138.    itmX.SubItems(2) = sIDType
  139.    itmX.SubItems(3) = sClass
  140.       
  141.    Listview_IndentItem Form2.ListView1.hwnd, CLng(itmX.Index), 1
  142.    
  143.    EnumChildProc = 1
  144.    
  145. End Function


  146. Private Function TrimNull(startstr As String) As String

  147.   Dim pos As Integer

  148.   pos = InStr(startstr, Chr$(0))
  149.   
  150.   If pos Then
  151.       TrimNull = Left$(startstr, pos - 1)
  152.       Exit Function
  153.   End If
  154.   
  155. 'if this far, there was
  156. 'no Chr$(0), so return the string
  157.   TrimNull = startstr
  158.   
  159. End Function


  160. Private Sub Listview_IndentItem(hwnd As Long, _
  161.                                 nItem As Long, _
  162.                                 nIndent As Long)

  163.    Dim LV As LVITEM

  164.   'if nIndent indicates that indentation
  165.   'is requested nItem is the item to indent
  166.    If nIndent > 0 Then
  167.       
  168.       With LV
  169.         .mask = LVIF_INDENT
  170.         .iItem = nItem - 1 'have to subtract 1
  171.         .iIndent = nIndent
  172.       End With
  173.       
  174.       Call SendMessage(hwnd, LVM_SETITEM, 0&, LV)
  175.       
  176.    End If
  177.       
  178. End Sub
  179. '--end block--'
  180. '------------------------------------------------------------------------------
  181. '
  182. ' Form Code
  183. '
  184. '------------------------------------------------------------------------------
  185. Option Explicit

  186. Private Sub Command1_Click()

  187.    ListView1.ListItems.Clear
  188.    Call EnumWindows(AddressOf EnumWindowProc, &H0)

  189. End Sub


  190. Private Sub Form_Load()

  191.    Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2

  192. End Sub


  193. Private Sub ListView1_DblClick()

  194.    Dim hwndSelected As Long
  195.    
  196.    hwndSelected = Val(ListView1.SelectedItem.Key)
  197.    
  198.    Load Form2
  199.    Call Form2.EnumSelectedWindow(ListView1.SelectedItem.Text, hwndSelected)
  200.    
  201. End Sub
  202. '--end block--'


  203. Form2 Code
  204.   
  205. Add the following code to Form2:

  206. --------------------------------------------------------------------------------

  207. Option Explicit

  208. Public Sub EnumSelectedWindow(sItem As String, hwnd As Long)

  209.    ListView1.ListItems.Clear
  210.    ListView1.ListItems.Add Text:=sItem, SmallIcon:="parent"
  211.    
  212.    Call EnumChildWindows(hwnd, AddressOf EnumChildProc, &H0)
  213.    
  214.    Me.Show vbModal
  215.    
  216. End Sub


  217. Private Sub Form_Load()

  218.    Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
  219.    
  220. End Sub
  221.   [/FONT]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-6 16:44 , Processed in 0.376483 second(s), 40 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表