[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Programmatically selecting text in Word of a particular format

Posted on 2006-10-27
9
Medium Priority
?
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Simple Linear Regression

649 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