16,553
社区成员
发帖
与我相关
我的任务
分享
Imports System.Windows.Forms
Imports JiebaNet.Segmenter
Imports Microsoft.Office.Interop.Word
Module GetWords
Public Function My_GetWords() '快捷键ALT+D后执行,获得待处理的句子
My_GetWords = ""
Dim app As Word.Application = Globals.ThisAddIn.Application '当前文档
Dim DocTxt As String
Dim segments As IEnumerable(Of String) '泛型,每个元素都为String类型
Dim a As Integer = app.Selection.Start
Dim segmenter = New JiebaSegmenter()
Try
'If app.ActiveDocument.Range.Words.Count >= 15 Then '如果文章字数大于15个,则始终取得光标之前的15个字
If app.ActiveDocument.Range.ComputeStatistics(WdStatistic.wdStatisticWords) >= 15 Then
DocTxt = app.ActiveDocument.Range(a - 15, a).Text '获取光标前面的字符
segments = segmenter.CutForSearch(DocTxt)
My_GetWords = String.Join("/", segments)
app.Selection.TypeText(My_GetWords)‘此处出问题。
'MessageBox.Show("【精确模式】:{0}", String.Join("/", segments)) 输出测试同样的问题。
Else
Return "未知"
End If
Catch e As System.NullReferenceException
Finally
End Try
End Function
End Module