Link to home
Start Free TrialLog in
Avatar of stitu
stitu

asked on

How can we identify font styles of a MS Word document template?

How can we identify font styles of a MS Word document template? I need to get the styles like H1, H2, etc and save the corresponding style configuration(font, size, color, weight etc) in the DB. How is this possible?
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

This macro will print to the Immediate window.

I am supposing that you know how to store the data in a database instead of printing it.
 
Dim doc As Word.Document
Dim sty As Word.Style
Set doc = Application.Documents.Open("c:\program files\microsoft office\templates\normal.dot")
For Each sty In doc.Styles
    Debug.Print sty.NameLocal
    Debug.Print sty.Font.Name
    Debug.Print sty.Font.Size
    Debug.Print sty.Font.etcetera
Next sty
Avatar of stitu
stitu

ASKER

The part of the problem is solved. Thanks a bunch. I am afraid I am not aware about managing this information into database and will be needing help on this one.
The assumption that I am making now is that you have created a Jet (Access) database with an appropriate table to receive the data.

This uses ADO, so you will need to make a reference to the
Microsoft Access Data Objects library (via Tools/References in your VBA window).

   Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
   
    Dim doc As Word.Document
    Dim sty As Word.Style
   
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\MyDataFolder\MyData.db;"
        rst.Open "MyFontsTable", cnn, adOpenDynamic, adLockPessimistic, adCmdTableDirect
        Set doc = Application.Documents.Open("c:\program files\microsoft office\templates\normal.dot")
            For Each sty In doc.Styles
                rst.AddNew
                rst.Fields("Style Name") = sty.NameLocal
                rst.Fields("Font Name") = sty.Font.Name
                rst.Fields("Font Size") = sty.Font.Size
               
                'Any other font properties that you want to save to the database
               
                rst.Update
            Next sty
        rst.Close
    cnn.Close
Avatar of stitu

ASKER

Ya. appropriate tables are ready in the database. How can we do it using DAO?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
stitu:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
Moderator, my recommended disposition is:

    Accept GrahamSkan's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer