Solved

Programmatically selecting text in Word of a particular format

Posted on 2006-10-27
9
315 Views
Last Modified: 2010-08-05
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
Comment
Question by:mpoisson
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 2

Expert Comment

by:dominik-jesiolowski
ID: 17821939

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
 
LVL 5

Accepted Solution

by:
consulteware earned 500 total points
ID: 17822434
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
 

Author Comment

by:mpoisson
ID: 17832976
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 5

Expert Comment

by:consulteware
ID: 17833020
Just change the "Heading 2" to the Style that you want to find
0
 
LVL 4

Expert Comment

by:mattcarver
ID: 17834976
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
 
LVL 4

Expert Comment

by:mattcarver
ID: 17834988
>>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
 

Author Comment

by:mpoisson
ID: 17837376
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
 
LVL 5

Expert Comment

by:consulteware
ID: 17837458
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
 
LVL 5

Expert Comment

by:consulteware
ID: 17837474
But thanks anyway.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
strCopies  challenge 17 87
Path of Workbook 3 76
recursion example 16 125
VBSCRIPT string remove all characters to the right after extension 4 62
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question