• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

Programmatically selecting text in Word of a particular format

I have the need to programmatically select all the text of a specific format, i.e "Heading 2".  I then want to take the results and iterate through them and put them in a DB table.  We have tried using C# with MS interop libraries and cannot figure it out.  I am open to any kind of solution using the .NET framework.  

I need an answer ASAP as a deadline is fast approaching.  If you need more information please don't hesitate to ask.

Thanks.
0
mpoisson
Asked:
mpoisson
  • 4
  • 2
  • 2
  • +1
1 Solution
 
dominik-jesiolowskiCommented:

You can try recording a macro in Word to find particular style, then do a "Find next". And then copy the code of a newly recorded macro.

HTH
Regards
DJ
0
 
consultewareCommented:
Just try this.

This code create one file at C:\ with the name db.txt that you can import to any database that you want.

There is an "varbefore" variable to avoide the infinite loop to check for the "Heading 2" style, just check that that worked for me.


    Dim varbefore As String
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\db.txt", True)
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 2")
    Selection.Find.Execute
    Do While Selection.Text <> ""
        If varbefore = Selection.Text Then
            Exit Do
        End If
        varbefore = Selection.Text
        a.WriteLine Selection.Text
        Selection.Find.Execute
    Loop
    a.Close

0
 
mpoissonAuthor Commented:
To Dominik,

We tried that already and for whatever reason the "selection of the specific format" is not in the code.  

Just some more information, we are using Word 2003.

Thanks for the comment.


0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
consultewareCommented:
Just change the "Heading 2" to the Style that you want to find
0
 
mattcarverCommented:
Author,

Dominik's code works.  In Microsoft Word with your document open hit "ALT+F11" to open the VBA editor.  Paste in the following code outlined below.  

------ cut below this line -----
Sub exportToDB()
    Dim varbefore As String
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\db.txt", True)
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 2")
    Selection.Find.Execute
    Do While Selection.Text <> ""
        If varbefore = Selection.Text Then
            Exit Do
        End If
        varbefore = Selection.Text
        a.WriteLine Selection.Text
        Selection.Find.Execute
    Loop
    a.Close
End Sub
------ stop cutting here -----

To run the code you execute the macro named "exportToDB()" by navigating to "Tools->Macro->Macros...", highlighting the macro and clicking the "Run" button.   In addition, you may want to turn down macro security to medium.  In Microsoft Word, navigate to "Tools->Macro->Security...", select "Medium" as your macro security level and click the "OK" button.
0
 
mattcarverCommented:
>>Dominik's code works.  In Microsoft Word with your document open hit "ALT+F11" to open the VBA editor.  Paste in the following code outlined below.  
I meant consulteware's code works, not dominik's.
0
 
mpoissonAuthor Commented:
I accept the answer because it was a viable solution, but I wanted a .NET solution, not a VBA one.  I have since found a solution at this site, http://msmvps.com/blogs/jonathangreensted/archive/2004/12/07/22717.aspx that we are going to use.

Thanks.
0
 
consultewareCommented:
My main question was that you need a solution ASAP, then I get you a solution ASAP that is why.
I'm a .Net programmer and ASP.Net programmer I could get you a .Net solution too but will take a quite bit longer not 10 minutes like this.
0
 
consultewareCommented:
But thanks anyway.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now