如何用代码获取和清空VBA立即窗口内的文本

舉杯邀明月 2013-12-17 04:18:49
自己想了很久,试验了好几种方法,都未达到目的。很是郁闷…………

我想要的效果是:
 ⑴ 可以读取和清空VBA立即窗口全部文本。 这是第一步,但是已经没能完成了。
 ⑵ 可以获取窗口内有多少行文本,实现读取和删除指定行号的文本,以及可以将“插入点”定位到指定行。
  最基本的要能定到行开头,能定位到指定行的任意位置更好(当然是合法位置)。
   比如窗口内:现有文本内容共9行,那么可以将插入点定位到1到10的任意行开头
    (解释一下,9行文本,光标可以定到第10行的。手工操作:用鼠标点、或按箭头键都能把光标移到第10行)

现在的情况是:
  可以找到VBA立即窗口的窗口句柄。 已知它的窗口类名是: VbaWindow

  但现在的问题是,按找到的窗口句柄,用 SendMessage( ) 发送按键消息,没有起到想要的作用。
  用 GetWindowText( ) ,也只是得到“立即窗口”这四个字,没能取得窗口内的文本。

  很怀疑是: “VbaWindow”内另外再包含了一个 RichTextBox ,但无法得到它的句柄!

  现在发个贴子,看能不能找到高人解决我的问题。最想要的是解决上面⑴中所说,⑵万一无法解决也没关系了。

  因为是要在VBA中使用,所以尽量不要用“远程注入”之类的技术来做。这方面我还没有实际操作过,担心代码移植
上会有问题。

  最好的代码是用 VB6/VBA +Win32 API 来解决,用C++、Delphi 的代码也行。
  用 .NET 类库的代码就算了……,我最讨厌 .NET Framework ,并且这种代码移植到 VBA中的可能性也几乎为0。

  可以直接从 VBA“立即窗口”的句柄开始写(直接写入在你的机子上实际的窗口句柄值),找窗口的过程略去。

  没有应用过 VBA的,可以打开 Word、Excel什么的(要用M$的产品),然后按 Alt + F11 打开 VBA编辑器。
如果立即窗口没有显示出来,可以按一下 Ctrl + G 把窗口打开(默认情况,它是内联窗口,你注意下屏幕下方),
然后随便在“立即窗口”内粘贴几行文本,进行试验。

  这个贴子先挂100分吧(我在这儿最多也只能发100分的),解决问题后再加分结贴(万一不能加,我另外开贴子)。

 期待能有大神帮我解决问题………………  
...全文
2008 47 打赏 收藏 转发到动态 举报
写回复
用AI写文章
47 条回复
切换为时间正序
请发表友善的回复…
发表回复
舉杯邀明月 2014-04-08
  • 打赏
  • 举报
回复
确实是没有得到满意答案。 只是我从来不会用“无满意结贴”来处理贴子。 就当散分了……
赵4老师 2014-04-08
  • 打赏
  • 举报
回复
引用 46 楼 Chen8013 的回复:
结贴了。
看这表情,应属“无满意结帖”。
舉杯邀明月 2014-04-08
  • 打赏
  • 举报
回复
结贴了。
knitime 2014-01-07
  • 打赏
  • 举报
回复
这个水贴很有质量,决定水个彻底
  • 打赏
  • 举报
回复
引用 35 楼 zhao4zhong1 的回复:
[quote=引用 31 楼 Chen8013 的回复:] [quote=引用 28 楼 zhao4zhong1 的回复:] ①你只要一手动让其显示,让我看里面的内容,我就说我在.ahk脚本里面也可以做到让其显示,取其中的内容;你说我再手动隐藏,我也可以让.ahk脚本取完内容后隐藏。你再说“人隐藏后是有内容的,你不能显示出来取内容后再隐藏”,我坚持说隐藏后其实没内容,转步骤① ↑ 加“”修改一处语病。
你这样的方式,也许我不用你的什么ahk脚本,能通过VBA代码操作VBE来搞呢。 把VBE激活到前台,模拟键盘Ctrl+G,让立即窗口显示出来。 获取VBA窗口的大小、位置等参数。 然后按立即窗口的位置、大小,用模拟“鼠标+键盘”操作,应该也能全部复制和清空内容了。 只是我不想走这条路,这个以前就考虑过的了。[/quote] 个人建议: 有很多看似可以走通的路摆在你面前,在最终选择走哪条路之前,建议不要把太多精力浪费在挑选走哪条路上,先能用最少的代价走通一次再说。[/quote] 过来人啊!这怎么变成水帖了?
赵4老师 2014-01-07
  • 打赏
  • 举报
回复
引用 43 楼 Chen8013 的回复:
[quote=引用 41 楼 u013064952 的回复:] 过来人啊!这怎么变成水帖了?
我发这个贴子,目的是找一个“后台操作”的实现方法。 把那个窗口“弄到前台来操作”,不是我想要的。 [/quote] 提供一个思路:在另一个看不见的桌面上把那个窗口“弄到前台来操作”,也可以看作是一种后台操作。 CreateDesktop The CreateDesktop function creates a new desktop on the window station associated with the calling process. It returns a handle that can be used to access the new desktop. The calling process must have an associated window station, either assigned by the system at process creation time or set by SetProcessWindowStation. A desktop is a secure object contained within a window station object. A desktop has a logical display surface and contains windows, menus, and hooks. HDESK CreateDesktop( LPCTSTR lpszDesktop, // name of the new desktop LPCTSTR lpszDevice, // reserved; must be NULL. LPDEVMODE pDevMode, // reserved; must be NULL DWORD dwFlags, // flags to control interaction with other // applications DWORD dwDesiredAccess, // specifies access of returned handle LPSECURITY_ATTRIBUTES lpsa // specifies security attributes of // the desktop ); Parameters lpszDesktop Pointer to a null-terminated string specifying the name of the desktop to be created. Desktop names are case-insensitive and may not contain backslash characters (\). lpszDevice Reserved; must be NULL. The desktop uses the default display driver loaded at boot time. pDevMode Reserved; must be NULL. dwFlags A bit flag parameter that controls how the calling application will cooperate with other applications on the desktop. This parameter can specify zero or the following value: Value Description DF_ALLOWOTHERACCOUNTHOOK Allows processes running in other accounts on the desktop to set hooks in this process. dwDesiredAccess Specifies the access rights the returned handle has to the desktop. This parameter must include the DESKTOP_CREATEWINDOW flag because internally CreateDesktop uses the handle to create a window. In addition, you can specify any of the standard access rights, such as READ_CONTROL or WRITE_DAC, and a combination of the following desktop-specific access rights. Value Description DESKTOP_CREATEMENU Required to create a menu on the desktop. DESKTOP_CREATEWINDOW Required to create a window on the desktop. DESKTOP_ENUMERATE Required for the desktop to be enumerated. DESKTOP_HOOKCONTROL Required to establish any of the window hooks. DESKTOP_JOURNALPLAYBACK Required to perform journal playback on the desktop. DESKTOP_JOURNALRECORD Required to perform journal recording on the desktop. DESKTOP_READOBJECTS Required to read objects on the desktop. DESKTOP_SWITCHDESKTOP Required to activate the desktop using the SwitchDesktop function. DESKTOP_WRITEOBJECTS Required to write objects on the desktop. lpsa Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpsa is NULL, the handle cannot be inherited. The lpSecurityDescriptor member of the structure specifies a security descriptor for the new desktop. If lpsa is NULL, the desktop inherits its security descriptor from the parent window station. Return Values If the function succeeds, the return value is a handle to the newly created desktop. If the specified desktop already exists, the function succeeds and returns a handle to the existing desktop. When you are finished using the handle, call the CloseDesktop function to close it. If the function fails, the return value is NULL. To get extended error information, call GetLastError. Remarks The CreateDesktop function returns a handle that can be used to access the desktop. If the dwDesiredAccess parameter specifies the READ_CONTROL, WRITE_DAC, or WRITE_OWNER standard access rights to access the security descriptor of the desktop object, you must also request the DESKTOP_READOBJECTS and DESKTOP_WRITEOBJECTS access rights. QuickInfo Windows NT: Requires version 3.51 or later. Windows: Unsupported. Windows CE: Unsupported. Header: Declared in winuser.h. Import Library: Use user32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also Window Stations and Desktops Overview, Window Station and Desktop Functions, CloseDesktop, SECURITY_ATTRIBUTES, SetProcessWindowStation, SwitchDesktop
舉杯邀明月 2014-01-07
  • 打赏
  • 举报
回复
引用 41 楼 u013064952 的回复:
过来人啊!这怎么变成水帖了?
我发这个贴子,目的是找一个“后台操作”的实现方法。 把那个窗口“弄到前台来操作”,不是我想要的。
mousestuman 2014-01-06
  • 打赏
  • 举报
回复
找不到贴回,就这个吧
孙家沟阿祖 2014-01-06
  • 打赏
  • 举报
回复
舉杯邀明月 2014-01-06
  • 打赏
  • 举报
回复
算了,我还是移到水园去,散分算了。
赵4老师 2013-12-23
  • 打赏
  • 举报
回复
当然有后台操作的方法了。 ReadProcessMemory
舉杯邀明月 2013-12-22
  • 打赏
  • 举报
回复
我就是想看看有没有能够进行“后台操作”的方法。 要不然,我发这个贴子干吗。
赵4老师 2013-12-19
  • 打赏
  • 举报
回复
引用 31 楼 Chen8013 的回复:
[quote=引用 28 楼 zhao4zhong1 的回复:] ①你只要一手动让其显示,让我看里面的内容,我就说我在.ahk脚本里面也可以做到让其显示,取其中的内容;你说我再手动隐藏,我也可以让.ahk脚本取完内容后隐藏。你再说“人隐藏后是有内容的,你不能显示出来取内容后再隐藏”,我坚持说隐藏后其实没内容,转步骤① ↑ 加“”修改一处语病。
你这样的方式,也许我不用你的什么ahk脚本,能通过VBA代码操作VBE来搞呢。 把VBE激活到前台,模拟键盘Ctrl+G,让立即窗口显示出来。 获取VBA窗口的大小、位置等参数。 然后按立即窗口的位置、大小,用模拟“鼠标+键盘”操作,应该也能全部复制和清空内容了。 只是我不想走这条路,这个以前就考虑过的了。[/quote] 个人建议: 有很多看似可以走通的路摆在你面前,在最终选择走哪条路之前,建议不要把太多精力浪费在挑选走哪条路上,先能用最少的代价走通一次再说。
赵4老师 2013-12-19
  • 打赏
  • 举报
回复
提醒:autohotkey是开源的。
赵4老师 2013-12-19
  • 打赏
  • 举报
回复
引用 32 楼 Chen8013 的回复:
不必要在我的面前把ahk捧得太高……
你只要举一个比ahk更强大的Windows系统级脚本能完成类似ahk支持的功能就行。
舉杯邀明月 2013-12-19
  • 打赏
  • 举报
回复
不必要在我的面前把ahk捧得太高……
舉杯邀明月 2013-12-19
  • 打赏
  • 举报
回复
引用 28 楼 zhao4zhong1 的回复:
①你只要一手动让其显示,让我看里面的内容,我就说我在.ahk脚本里面也可以做到让其显示,取其中的内容;你说我再手动隐藏,我也可以让.ahk脚本取完内容后隐藏。你再说“人隐藏后是有内容的,你不能显示出来取内容后再隐藏”,我坚持说隐藏后其实没内容,转步骤① ↑ 加“”修改一处语病。
你这样的方式,也许我不用你的什么ahk脚本,能通过VBA代码操作VBE来搞呢。 把VBE激活到前台,模拟键盘Ctrl+G,让立即窗口显示出来。 获取VBA窗口的大小、位置等参数。 然后按立即窗口的位置、大小,用模拟“鼠标+键盘”操作,应该也能全部复制和清空内容了。 只是我不想走这条路,这个以前就考虑过的了。
赵4老师 2013-12-19
  • 打赏
  • 举报
回复
引用 29 楼 Chen8013 的回复:
[quote=引用 22 楼 zhao4zhong1 的回复:] 窗口过程:0x6506870D 的代码在 C:\Program Files\Common Files\Microsoft Shared\vba\VBA6\VBE6.DLL 中,用W32Dasm软件反汇编看看。
一大片的汇编代码,我看不出什么明堂出来 ………… [/quote] 所以要学习.ahk嘛。学.ahk写个脚本完成功能⑴总比分析窗口过程对应的汇编,然后写个C/C++ Hook DLL完成同样功能简单的多吧。
舉杯邀明月 2013-12-19
  • 打赏
  • 举报
回复
引用 22 楼 zhao4zhong1 的回复:
窗口过程:0x6506870D 的代码在 C:\Program Files\Common Files\Microsoft Shared\vba\VBA6\VBE6.DLL 中,用W32Dasm软件反汇编看看。
一大片的汇编代码,我看不出什么明堂出来 …………
赵4老师 2013-12-19
  • 打赏
  • 举报
回复
①你只要一手动让其显示,让我看里面的内容,我就说我在.ahk脚本里面也可以做到让其显示,取其中的内容;你说我再手动隐藏,我也可以让.ahk脚本取完内容后隐藏。你再说“人隐藏后是有内容的,你不能显示出来取内容后再隐藏”,我坚持说隐藏后其实没内容,转步骤① ↑ 加“”修改一处语病。
加载更多回复(27)

741

社区成员

发帖
与我相关
我的任务
社区描述
VB 版八卦、闲侃,联络感情地盘,禁广告帖、作业帖
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧