从所选尾注生成的字符串中提取文本 [英] Extract text from string generated from selected endnotes

查看:62
本文介绍了从所选尾注生成的字符串中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Word中,当我选择一个段落时,我需要一个MsgBox来显示尾注内特定部分的列表(即每个显示一个新行).

In Word, when I select a paragraph, I need a MsgBox to display a list (i.e. a new line for each) of a certain section within an endnote.

下面的代码将显示一个MsgBox,其中包含选定内容中的尾注的完整文本,但是我只希望短语"Extracted material is from"和下一个分号之间的信息在MsgBox中列出. (下面的尾注示例,并以粗体显示所需的提取内容)

The below code will display a MsgBox with the FULL TEXT of endnotes within the selection, but I only want the information between the phrase "Extracted material is from " and the next semicolon to be listed in the MsgBox. (Example below of endnotes with desired extraction in bold)

尾注格式:

[1] Position1文本; Position2text; Position3text; Position4text; Position5text; Position6提取的材料来自食用//水果//苹果; Position7text; Position8text

[1] Position1text; Position2text; Position3text; Position4text; Position5text; Position6 Extracted material is from EDIBLE//FRUIT//APPPLES; Position7text; Position8text

[2] Position1text; Position2text; Position3text; Position4text; Position5text; Position6提取的材料来自 NONEDIBLE//家具//椅子; Position7text; Position8text

[2] Position1text; Position2text; Position3text; Position4text; Position5text; Position6 Extracted material is from NONEDIBLE//FURNITURE//CHAIR; Position7text; Position8text

我希望MsgBox读取:

I'd like the MsgBox to read:

此段包含:

食用//水果//苹果

不可折叠//家具//椅子

NONEDIBLE//FURNITURE//CHAIR

Sub TestEndNoteMsg()
Dim e As Endnote
Dim str As String
For Each e In Selection.Endnotes
    str = str & e.Range.Text
Next e
MsgBox str
End Sub

推荐答案

此代码未进行优化,因此您可以更好地了解其工作原理.您可以轻松地将InStr函数嵌套在一行中,但是很难理解.

This is not optimized so that you can better see how this works. You can just as easily nest the InStr functions into a single line, but it gets a little hard to follow.

在下面,您会发现您首先找到短语提取的材料来自"的位置,然后在该位置添加27个字符(字符串的长度-1).这样我们就可以开始提取字符了.

Below you can see that you first find the position of the phrase "Extracted material is from " and add 27 characters to the position (the length of the string - 1). That gives us the character position we want to start the extraction from.

然后使用该位置作为新起点,找到下一个分号并将该位置保存在lngEnd中.然后将str转换为仅提取所需的数据.

Then using this position as a new starting point, find the next semi-colon and save that position in lngEnd. Then transform str to extract only the data you want.

修改 忘了循环,我一直保护str,直到创建了已分析的字符串,然后将每个元素附加到变量中.我将str更改为e.Range.Text以仅处理当前的尾注处理,并避免截断前一个尾注.

Edit Forgot the loop, I am protecting str until the parsed string is created, then appending each element to the variable. I changed str to e.Range.Text to deal only with the current endnote processing and avoiding truncating the previous endnote.

Sub TestEndNoteMsg()
Dim e As Endnote
Dim str As String
Dim lngStart As Long
Dim lngEnd As Long
For Each e In Selection.Endnotes
    lngStart = InStr(1, e.Range.Text, "Extracted material is from ", 1) + 27
    lngEnd = InStr(lngStart, e.Range.Text, ";", 1)
    str = str & MID(e.Range.Text, lngStart, lngEnd - lngStart) & vbcrlf
Next e
MsgBox "This paragraph contains:" & vbcrlf & str
End Sub

这篇关于从所选尾注生成的字符串中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆