我正在写一个VSTO,它需要从电子邮件中获取所选文本并进行处理。我发现/修改了一些代码,这些代码非常适合从IFF电子邮件正文中获取纯文本。如果文本在表中,则似乎仅返回第一个选定的单元格。如果我尝试检查Application.Selection.Range.Cells,它将返回表的一部分,但包含选定的单元格,但不包括所有单元格。有人知道如何获取选定的单元格吗?以下是用于从电子邮件正文中获取选定文本的基本代码:
Dim objExplorer As Outlook.Explorer = Globals.ThisAddIn.Application.ActiveExplorer()
'Get the current MailItem
Dim mailItem As Outlook.MailItem = TryCast(objExplorer.Selection(1), Outlook.MailItem)
'Get the current TaskItem
Dim taskItem As Outlook.TaskItem = TryCast(objExplorer.Selection(1), Outlook.TaskItem)
'Get current ContactItem
Dim contactItem As Outlook.ContactItem = TryCast(objExplorer.Selection(1), Outlook.ContactItem)
'Get the current AppointmentItem
Dim appointmentItem As Outlook.AppointmentItem = TryCast(objExplorer.Selection(1), Outlook.AppointmentItem)
Dim inspector As Outlook.Inspector = Nothing
If (mailItem IsNot Nothing) Then
'Obtain Inspector object for the current MailItem
inspector = mailItem.GetInspector
ElseIf (taskItem IsNot Nothing) Then
'Obtain Inspector object for the current TaskItem
inspector = taskItem.GetInspector
ElseIf (contactItem IsNot Nothing) Then
'Obtain Inspector object for the current NoteItem
inspector = contactItem.GetInspector
ElseIf (appointmentItem IsNot Nothing) Then
'obtain Inspector object for the current AppointmentItem
inspector = appointmentItem.GetInspector
End If
If inspector Is Nothing Then
MsgBox("Please open an item to scan.", vbOKOnly + vbExclamation)
Return ""
End If
' Obtain the Document object from the Inspector object
Dim document As word.Document = inspector.WordEditor
'Store the selected text to a variable
Dim selectedText As String = If(document.Application.Selection IsNot Nothing, document.Application.Selection.Text, "")
答案 0 :(得分:0)
从问题开始
有人知道如何获得选定的细胞吗?
我了解用户会选择多个不连续的单元格,以期可以读取其内容。
这是不可能的; Word的对象模型无法处理多个选择,即使用户可以创建它们(由于相对较早,实际上-对象模型也从未遇到过这些选择)。
有必要将某种独特的字符格式应用于Application.Selection
,然后找到该格式的每个实例以提取内容。通常,可以在循环中使用“查找”来完成-循环部分可以拾取内容并删除格式。
下面的代码段说明了如何完成此操作。
Selection
是对象模型中唯一可以处理多个选定的非连续内容的部分。)最后,标记样式从文档中删除。
Dim doc As Word.Document = inspector.WordEditor
Dim styl As Word.Style = doc.styles.Add("MultiSelectMarker", _
Word.WdStyleType.wdStyleTypeCharacter)
styl.BaseStyle = Word.WdBuiltinStyle.wdStyleDefaultParagraphFont
Application.Selection.Style = styl
Dim rngFind As Word.Range = doc.Content
Dim selectedContent As String
With rngFind.Find
.Text = ""
.Format = True
.Style = styl
.Wrap = Word.WdFindWrap.wdFindStop
Do While .Execute
'Concatenate the non-contiguous selections' content
selectedContent = selectedContent & rngFind.Text & " "
If rngFind.Information(Word.WdInformation.wdWithInTable) Then
'Extend the range to include the cell structures so that
'applying the default style does not result in Find.Execute = true
rngFind.Cells(1).Range.Style = Word.WdBuiltinStyle.wdStyleDefaultParagraphFont
Else
rngFind.Style = Word.WdBuiltinStyle.wdStyleDefaultParagraphFont
End If
'Move outside the found Range so that the search continues
rngFind.Collapse wdCollapseEnd
Loop
End With
Debug.Print(selectedContent)
styl.Delete