【问题标题】:How do I locate a Word application window?如何找到 Word 应用程序窗口?
【发布时间】:2008-09-14 12:48:45
【问题描述】:

我有一个 VB.net 测试应用程序,它单击打开 Microsoft Word 应用程序窗口并显示文档的链接。如何找到 Word 应用程序窗口以便从中获取一些文本?

【问题讨论】:

  • 与您定位任何其他应用程序窗口的方式完全相同?

标签: windows vb.net interop ms-word ms-office


【解决方案1】:

您可以使用 Word COM 对象打开工作文档,然后对其进行操作。请务必先添加 Microsoft Word 的引用。

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim strFileName As String
Dim wordapp As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document

Try
    doc = wordapp.Documents.Open("c:\testdoc.doc")
    doc.Activate()

Catch ex As COMException

    MessageBox.Show("Error accessing Word document.")

End Try

End Sub

End Class

doc 对象是您创建的 Word 实例的句柄,您可以使用所有常规选项(保存、打印等)。你可以用 wordapp 做同样的事情。一个技巧是使用 Word 中的宏编辑器来记录您想要执行的操作。然后您可以在宏编辑器中查看它。这为您的 VB 代码提供了一个很好的起点。

另外,请务必在最后处理 Word COM 对象。

【讨论】:

    【解决方案2】:

    我在我的博客上发布了一个 SourceSafe 对话框,我做了类似的事情。基本上,我使用 Spy++ 或 Winspector 来找出窗口类名,然后调用 Win32 来处理窗口。我已经把源放在我的博客上:http://harriyott.com/2006/07/sourcesafe-cant-leave-well-alone.aspx

    【讨论】:

      【解决方案3】:

      你想activate这个词吗?如果您想要完全控制,您需要从您的 vb.net 应用程序中自动执行单词。在这里查看一些示例:12

      【讨论】:

        猜你喜欢
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-09
        • 2011-12-21
        相关资源
        最近更新 更多