如何找到Word应用程序窗口?

时间:2021-11-27 20:28:02

I have a VB.net test application that clicks a link that opens the Microsoft Word application window and displays the document. How do I locate the Word application window so that I can grab some text from it?

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

3 个解决方案

#1


1  

You can use the Word COM object to open the work document and then you manipulate it. Make sure to add a reference for Microsoft Word first.

您可以使用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

The doc object is a handle for the instance of Word you have created and you can use all the normal options (save, print etc). You can do likewise with the wordapp. A trick is to use the macro editor in Word to record what you want to do. You can then view this in the Macro Editor. This give you a great starting point for your VB code.

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

Also, be sure to dispose of the Word COM objects at the end.

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

#2


1  

I've done something similar with a SourceSafe dialog, which I posted on my blog. Basically, I used either Spy++ or Winspector to find out the window class name, and make Win32 calls to do stuff with the window. I've put the source on my blog: http://harriyott.com/2006/07/sourcesafe-cant-leave-well-alone.aspx

我已经在SourceSafe对话框中做了类似的事情,我在博客上发布了这个对话框。基本上,我使用Spy ++或Winspector来查找窗口类名,并使Win32调用来执行窗口的操作。我把源代码放在我的博客上:http://harriyott.com/2006/07/sourcesafe-cant-leave-well-alone.aspx

#3


1  

Are you trying to activate the word app? If you want full control, you need to automate word from your vb.net app. Check here for some samples: 1, 2

您是否尝试激活应用这个词?如果您想要完全控制,您需要从您的vb.net应用程序自动化word。点击这里查看一些样品:1,2

#1


1  

You can use the Word COM object to open the work document and then you manipulate it. Make sure to add a reference for Microsoft Word first.

您可以使用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

The doc object is a handle for the instance of Word you have created and you can use all the normal options (save, print etc). You can do likewise with the wordapp. A trick is to use the macro editor in Word to record what you want to do. You can then view this in the Macro Editor. This give you a great starting point for your VB code.

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

Also, be sure to dispose of the Word COM objects at the end.

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

#2


1  

I've done something similar with a SourceSafe dialog, which I posted on my blog. Basically, I used either Spy++ or Winspector to find out the window class name, and make Win32 calls to do stuff with the window. I've put the source on my blog: http://harriyott.com/2006/07/sourcesafe-cant-leave-well-alone.aspx

我已经在SourceSafe对话框中做了类似的事情,我在博客上发布了这个对话框。基本上,我使用Spy ++或Winspector来查找窗口类名,并使Win32调用来执行窗口的操作。我把源代码放在我的博客上:http://harriyott.com/2006/07/sourcesafe-cant-leave-well-alone.aspx

#3


1  

Are you trying to activate the word app? If you want full control, you need to automate word from your vb.net app. Check here for some samples: 1, 2

您是否尝试激活应用这个词?如果您想要完全控制,您需要从您的vb.net应用程序自动化word。点击这里查看一些样品:1,2