VB.NET还是比较常用的,于是我研究了一下VB.NET word查文字,在这里拿出来和大家分享一下,希望对大家有用。
VB.NET word查文字代码:
- Public Class ThisDocument
- Private MyDocMenu As Office.CommandBarButton
- Private missing = System.Reflection.Missing.Value
- '在功能区中新增菜单组
- Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
- Dim MyCommandBarPopup As Office.CommandBarPopup = Nothing
- Dim MyCommandBarMenu As Office.CommandBar = CType( _
- Application.CommandBars.ActiveMenuBar, Office.CommandBar)
- Dim MyControlsCount As Integer = MyCommandBarMenu.Controls.Count
- MyCommandBarPopup = CType(MyCommandBarMenu.Controls.Add( _
- Office.MsoControlType.msoControlPopup, missing, missing, _
- MyControlsCount, True), Office.CommandBarPopup)
- If (MyCommandBarPopup IsNot Nothing) Then
- MyCommandBarPopup.Caption = "文件管理"
- MyDocMenu = CType(MyCommandBarPopup.Controls.Add( _
- Office.MsoControlType.msoControlButton, missing, _
- missing, missing, True), Office.CommandBarButton)
- MyDocMenu.Caption = "查找文字"
- AddHandler MyDocMenu.Click, AddressOf MyDocMenuCommand_Click
- End If
- End Sub
- Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
- End Sub
- '查找文字
- Private Sub MyDocMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
- Dim MyFind As String = ""
- Dim MyFindForm As New Form1()
- MyFindForm.ShowDialog()
- MyFind = MyFindForm.textBox1.Text
- If (MyFind.Length < 0) Then
- Return
- End If
- Dim MyCount As Integer = 0
- Dim MyDocument As Word.Document = Me.Application.ActiveDocument
- Dim MyRange As Word.Range = MyDocument.Content
- MyRange.Find.ClearFormatting()
- MyRange.Find.Forward = True
- MyRange.Find.Text = MyFind
- MyRange.Find.Execute(missing, missing, missing, missing, missing, _
- missing, missing, missing, missing, missing, _
- missing, missing, missing, missing, missing)
- While (MyRange.Find.Found)
- MyRange.Bold = 1
- MyCountMyCount = MyCount + 1
- MyRange.Find.Execute(missing, missing, missing, missing, _
- missing, missing, missing, missing, missing, missing, _
- missing, missing, missing, missing, missing)
- End While
- MessageBox.Show(MyFind + "一共找到:" + MyCount.ToString() + "个!", _
- "51cto信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
- End Sub
- End Class
以上是VB.NET word查文字的一个案例,大家可以试试。
【编辑推荐】