Solved

Programmatically selecting text in Word of a particular format

Posted on 2006-10-27
9
312 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
 
LVL 5

Expert Comment

by:consulteware
ID: 17833020
Just change the "Heading 2" to the Style that you want to find
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now