返回特定字符串后的MS word文档行的一部分 [英] Return a part of a MS word document line after specific string

查看:102
本文介绍了返回特定字符串后的MS word文档行的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个问题是返回MS Word文档行的一部分。我用来搜索word doc内容的特定字符串是在:之后。



示例:

我在寻找什么for String1:这不是我想要在VB2010中返回的内容。

我正在寻找的是String2:这不是我想要在VB2010中返回的内容。

我正在寻找的是String3:这就是我想在VB2010中返回的内容。

我正在寻找的是String4:这不是我想要返回的内容VB2010。

我要找的是String5:这不是我想在VB2010中返回的内容。



搜索字符串是:String3 :



我尝试过:



Hello to all,
I have a problem to return a part of MS Word Document line. The specific string that I am using to search the content of word doc is after ":".

Example:
What I am looking for is String1: This is not what I want to return in VB2010.
What I am looking for is String2: This is not what I want to return in VB2010.
What I am looking for is String3: This is what I want to return in VB2010.
What I am looking for is String4: This is not what I want to return in VB2010.
What I am looking for is String5: This is not what I want to return in VB2010.

Search string is: String3:

What I have tried:

Dim findText As String = "String3:"

oWord.Selection.Find.ClearFormatting()
If oWord.Selection.Find.Execute(findText) = True Then
    MessageBox.Show("Text found.")
'In the current line, how to return text after the found string?
Else
    MessageBox.Show("The text could not be located.")
End If



非常感谢任何帮助。


Any help is very appreciated.

推荐答案

您需要学习RegEx(常规表达式)并使用找到匹配项的函数。

您需要的RegEx将是比如String3 :(。+)
You need to learn RegEx (regular Expressions) and use the function that find matches.
the RegEx you need will be something like "String3:(.+)






以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

.NET Regex Tester - Regex Storm [ ^ ]

Expresso正则表达式工具 [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]



[更新]

请将问题清楚地说明你想要的内容。

编写一个在MSWord文档上做某事的程序自动化MSWord是两回事。

Graeme_Grant的Solution2就是你想要的。
"

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]

[Update]
Please make the question clear about what you want.
Writing a program that do something on MSWord document and automating MSWord are 2 different things.
Solution2 from Graeme_Grant is what you want.


了解如何在MS Word或MS Excel中自动执行任务的最佳方法是记录一个执行您想要实现的宏,然后查看生成的VBA代码。



现在你转到VB2010代码并发出与MS Word宏录制器相同的命令。







以下是为您完成的第一部分:
The best way to learn how to automate tasks in MS Word or MS Excel is to record a macro that does what you want to achieve, then view the VBA code generated.

Now you and go to your VB2010 code and issue the same commands that the MS Word macro recorder generated for you.

[edit:]

Here is the first part done for you:
Dim TextToFind As String
TextToFind = "String3:"

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .text = TextToFind
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute

找到标记后,需要找到文本的开头和结尾你想要合作。我有一些代码可以向您展示如何在Word文档中移动并选择文本: TryParseNumber [ ^ ]功能。它不会完全符合您的要求,但会指向正确的方向。

Once you have found the marker, you need to find the beginning and end of the text that you want to work with. I have some code that can show you how to move around and select text in a Word Document: TryParseNumber[^] function. It won't do exactly what you want but will point you in the right direction.


这篇关于返回特定字符串后的MS word文档行的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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