Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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.
Posted
Updated 16-Mar-17 2:38am
v2
Comments
Graeme_Grant 16-Mar-17 4:23am    
Are you looking for a phrase in a Word document from VB.Net? or from Word VBA?
blueye89 16-Mar-17 4:28am    
I am using VB2010.
Graeme_Grant 16-Mar-17 4:30am    
and you are working with a Word Document from VB2010?
blueye89 16-Mar-17 4:38am    
Yes!
Graeme_Grant 16-Mar-17 4:39am    
Please update your question as it was unclear. Solution 1 is an example of that confusion.

You need to learn RegEx (regular Expressions) and use the function that find matches.
the RegEx you need will be something like "String3:(.+)$"

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.
 
Share this answer
 
v3
Comments
blueye89 16-Mar-17 4:42am    
Is there something easier? I am not familiar with the type of code that you posted via links.
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
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.
 
Share this answer
 
v3
Comments
blueye89 16-Mar-17 4:55am    
I have tried it, steps are:
1. Start Recording
2. Ctrl+F
3. Paste Search String
4. Click on Find Button
What Next? It has been done also by using code above. How to send the content after to VB2010 MsgBox?
Graeme_Grant 16-Mar-17 5:08am    
I have updated the solution.
Here is something that works fine.
Public Function GetDocNoFromDoc() As String
      Dim strDocNoFromDoc As String
      Dim rng As Word.Range

      rng = WordApp.ActiveDocument.Range

      With rng.Find
          .ClearFormatting()
          .Text = "DocNo:"
          If .Execute() Then
              rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
              rng.End = rng.End + 7
              strDocNoFromDoc = rng.Text
          End If
      End With

      Return strDocNoFromDoc
  End Function
 
Share this answer
 
I am struggling with another problem which is maybe connected with this topic. I would like to find text which is bold and to return it to textbox. Any idea, please?
Maybe to return all characters which are bold?
 
Share this answer
 
Comments
Graeme_Grant 16-Mar-17 8:44am    
What is the end text marker?
blueye89 16-Mar-17 8:54am    
When I enable paragraph marks.
https://s12.postimg.org/ev2qlsr65/img.png
Graeme_Grant 16-Mar-17 8:55am    
blueye89 16-Mar-17 9:07am    
I have done it manually, using:
For Each w As Word.Range In oDoc.Words
If w.Font.Bold = True Then
txtEngDrwPath.Text = w.Text
End If
Next

But this returns me only last bold word in the row, with all spaces after that I don't need.
Graeme_Grant 16-Mar-17 9:10am    
charmoved = Selection.EndOf(Unit:=wdParagraph, Extend:=wdExtend)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900