Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help 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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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