Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Tables and Figures Macro (XP / Word 2003)

    Hello,

    I am trying to create a Macro that will look inside a folder with hundreds of word documents and pull out all the "Chapter Title", "Figure Title" and "Table Title" styles. The Macro will then generate generate a new document filled with all these entries. I will then be able to use this list for my table of contents. The output will look something like this:

    Chapter........................................... ......1
    Figure 1. XYZ...................................1-2
    Figure 2. ZZZ....................................1-2
    Table 1. XYZ....................................1-3

    Chapter........................................... ......2
    Figure 1. XYZ...................................2-2
    Figure 2. ZZZ....................................2-2
    Table 1. XYZ....................................2-3

    Any assistance on this coding is very much appreciated.

    Regards,

    Jim

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    The following macro will loop through all documents in a specified folder, and insert an RD field for each of them into a new document. Finally it will insert a TOC field using the three styles you mention.

    Sub LoopFolder()
    ' Modify as needed, but keep trailing backslash
    Const strPath = "Cocs"

    Dim strFile As String
    Dim docNew As Document
    Dim rng As Range

    On Error GoTo ErrHandler

    Application.ScreenUpdating = False

    ' New doc
    Set docNew = Documents.Add
    ' Loop through docs in folder
    strFile = Dir(strPath & "*.doc")
    Do While Not strFile = ""
    ' End of new doc
    Set rng = docNew.Range
    rng.Collapse Direction:=wdCollapseEnd
    ' Insert RD field
    docNew.Fields.Add rng, wdFieldRefDoc, Chr(34) & _
    Replace(strPath & strFile, "", "") & Chr(34)
    ' On to the next
    strFile = Dir
    Loop
    ' End of doc
    Set rng = docNew.Range
    rng.Collapse Direction:=wdCollapseEnd
    ' Insert TOC field
    docNew.Fields.Add rng, wdFieldTOC, "/t " & Chr(34) & _
    "Chapter Title,1,Figure Title,1,Table Title,1" & Chr(34)

    ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  3. #3
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    Hello Hans,

    Thanks for the detailed response. I really appreciate it.
    I did try to run the Macro you provided, but I am getting the following error message:



    strFile = Dir(strPath & "*.doc")
    Compile error: Constant expression required



    I am pointing to the correct folder on my "C" drive.
    Any reason why this error is occurring?
    Thanks again for the support.

    Regards,

    Jim

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    I tested the code on a folder with Word documents, so I know it should work. What is the exact line in your code that defines strPath?

  5. #5
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    Hello Hans,

    Thanks for the quick response.
    I am now actually having some measure of success, but not full success.
    I have over 100 word documents that are tied to a specific template and protected (locked down). This keeps out unwanted styles.
    The documents are all in the same folder.

    Anyway, I need to generate a TOC output that will look like this hypothetical example:
    -------------------------------------------------------------------------------------------------------------------------------

    Chapter 1 GENERAL INFORMATION
    Router General Information....................................... .................................................. 0100
    Figure 1. Router Front View.............................................. ..............................0100-1
    Router Descrption and Data.............................................. .........................................0102
    Figure 1. Exterior Components........................................ ..............................0102-2
    Table 1. Difference Between Models............................................ .................0102-3
    Figure 2. Interior Components........................................ ................................0102-4
    Introduction...................................... .................................................. .............................0200
    Router Theory of Operation......................................... ................................................03 00
    XYZ Theory of Operation......................................... .................................................. ...0400

    -------------------------------------------------------------------------------------------------------------------------------

    I use a finite amount of styles in each protected word document - something like this:

    - Chapter Title
    - Chapter Number
    - Figure Title (the figures restart within each chapter)
    - Table Title (the figures restart within each chapter)

    The macro is able to display the top level document, but not the Figure Titles and Table Titles in each individual document. Is this because the documents are protected?

    Is there a way to get this working? I think I am on the right track now.

    Can I subsitute any style from my template in this line of code: "Chapter Title,1,Figure Title,1,Table Title,1" & Chr(34) ??
    I would want the Chapter number (e.g. 0200) to go right after the Chapter Title on the page

    Thanks for your assistance.

    Jim

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    The instruction that inserts the TOC field

    docNew.Fields.Add rng, wdFieldTOC, "/t " & Chr(34) & _
    "Chapter Title,1,Figure Title,1,Table Title,1" & Chr(34)

    contains a list of the styles to be included, each one followed by the level. In the above example, Chapter Title, Figure Title and Table Title will all be included in the TOC at level 1. If you want the figures and tables to be subordinate to the chapters, use level 2 for them:

    docNew.Fields.Add rng, wdFieldTOC, "/t " & Chr(34) & _
    "Chapter Title,1,Figure Title,2,Table Title,2" & Chr(34)

    You can add other styles. I don't think you can have two TOC entries for different styles on the same line; that would require "unlinking" the TOC after creating it and then to postprocess it.

  7. #7
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    Hello Hans,

    Is it possible that you can send me a two-three page generic word document, that I can put into a folder (e.g. CDocs) and then run the macro and generate: Chapter Title, Chapter Number, Figure Title (and page), Table Title (and page)? I have tried changing the (1) to (2), but the Macro will not show pull in any Figure Titles or Table Titles and I am not sure why. Any assistance you can provide on this, is very much appreciated. Regards, Jim BTW - Also, please note that I am not a VBA guru, so if you could spell out all the steps, that would be very helpful. Thanks again!

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    The attached zip file contains two documents:

    1) TestDoc.doc - place this document by itself in a folder Cocs (or in another folder, but then you must modify strPath in the folder accordingly). Note: I put some lines in each of the three styles into the document, but I didn't include any tables or pictures to keep the file size down.
    2) MacroDoc.doc - make sure that your macro security is not set to High, then open this document, enable macros and click the button labeled "Click Here".
    Attached Files Attached Files

  9. #9
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    Hello Hans,
    Thanks for the template, it works great, but unfortunately, it will not work on any of my documents. I am getting the Chapter Title and nothing else. All of my documents point to the same
    template (e.g. XYZ.dot). I have a generic sample here that I am hoping you can look at. Perhaps additional coding is required to work with this template. Any input is very much welcomed. Thanks, Jim
    Attached Files Attached Files

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    The macro expects the exact style names. You mentioned "Figure Title" and "Table Title" in the first post in this thread, but the corresponding styles in your template are named "Fig_Title" and "Table_Title". You should use the latter names in the macro:

    docNew.Fields.Add rng, wdFieldTOC, "/t " & Chr(34) & _
    "Chapter Title,1,Fig_Title,2,Table_Title,2" & Chr(34)

    Note: I don't see a style "Chapter Title" in the template - did you mean the Heading 1 style (with alias Chapter)?

  11. The Following User Says Thank You to HansV For This Useful Post:

    brjeremy (2015-01-01)

  12. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    Ah, I see what you mean.

    Apparently, Word ignores the specified styles when creating a TOC based on RD fields, and uses the outline levels instead. In the sample document I created, I had assigned outline levels to the Figure Title etc. styles, so they were included in the TOC anyway.
    Since you are working with existing documents, it is not attractive to go and change the outline level of the styles in all those documents manually. You can do it using a macro, but it will be slow if you have thousands of documents.

    Sub UpdateLevels()
    Const strPath = "Cocs"

    Dim strFile As String
    Dim doc As Document

    On Error GoTo ErrHandler

    Application.ScreenUpdating = False

    ' Loop through docs in folder
    strFile = Dir(strPath & "*.doc")
    Do While Not strFile = ""
    Set doc = Documents.Open(strPath & strFile)
    On Error Resume Next
    doc.Styles("Fig_Title").ParagraphFormat.OutlineLev el = wdOutlineLevel2
    doc.Styles("Table_Title").ParagraphFormat.OutlineL evel = wdOutlineLevel2
    ' Add more styles if needed
    On Error GoTo ErrHandler
    If doc.Saved = False Then
    doc.Save
    End If
    doc.Close SaveChanges:=False
    strFile = Dir
    Loop

    ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    After running this macro, the TOC created by LoopFolder should pick up all styles with the appropriate level.

  13. #12
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    Hello Hans,

    Sorry for the delay in getting back. I am using the following styles when I try to generate a TOC:

    Heading 2,Work_Package
    Fig_Title
    Table_Title

    It runs and picks up the first style, but still refuses to pick up the second style and third style, when the Macro is run. I will go ahead and try it again today. I tried it briefly last night when half awake...
    Doe is make a difference in the name of the style? In other words, could it stop working if it sees a space or underscore in the style name? Is it case sensitive? I am just trying to figure out why it will not work.
    The only other thing to note is that I have a hidden text style (invisible white text) sitting in front of Heading 2, Work_Package that allows me to put in a chapter number (in invisible white text) that is displayed when I go to put the word document in a PDF and generate bookmarks (e.g. 0001 XYZ Operation). Normally each chapter is shown as just the title, with the chapter number up in the page_header, based on the specification I am following. But, I do not think this makes a difference as it generates the first of the three styles without a problem. The bottom line is that it does not look for or cannot pickup Fig_Title and Table_Title, when generating an output. I really need to have those styles picked up in the TOC too. The end result should look something like this:

    Chapter 1 GENERAL INFORMATION
    Router General Information....................................... .................................................. 0100
    Figure 1. Router Front View.............................................. ..............................0100-1
    Router Descrption and Data.............................................. .........................................0102
    Figure 1. Exterior Components........................................ ..............................0102-2
    Table 1. Difference Between Models............................................ .................0102-3
    Figure 2. Interior Components........................................ ................................0102-4
    Introduction...................................... .................................................. .............................0200
    Router Theory of Operation......................................... ................................................03 00
    XYZ Theory of Operation......................................... .................................................. ...0400


    I am hoping this will be possible, otherwise I will have to do manually pull out this information in around 200 word documents.

    Thanks for the continued support on this nagging issue.

    Jim

  14. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    The screenshot below shows what the TOC looks like when I do the following:
    1) I create a document based on your template and store it in Cocs.
    2) I run the UpdateLevels macro.
    3) I run the LoopFolder macro.
    If I had added more styles in the UpdateLevels macro, more lines would have appeared in the TOC.
    Style names are not case sensitive, and AFAIK, it doesn't matter whether the style name contains spaces or underscores.
    Attached Images Attached Images
    • File Type: png x.PNG (5.5 KB, 3 views)

  15. #14
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    Hello Hans,

    I still had some problems with getting it to work, but it looks like it is finally working now. I have one other question for you, if I may? Do you know how I can get the output to show up like this:

    XYZ Operation......................................... ....0100 (where 0100 is the name of the chapter and not the page number) ?

    Right now it comes in as:

    0100 XYZ Operation (the 0100 mentioned here is a hidden text style - white text that I originally used for creating bookmarks in Adobe Acrobat). I think of this like a command that is issued to print out x & y on the same line. Currently, my Hidden style and Heading 2, Work_Package style are on the same line in the Word Document. Is there a way to swap the output around so that the following is generated:

    "Heading 2, Work_Package style" -----------> XYZ Operations........................................ ...................0100 <--------------------- "hidden text style"
    "Fig_Title style" ---------------------------------> Figure 1. XYZ Front View...........................................010 0-1 <--------------------- "page footer style"
    "Table Title style" ---------------------------------> Table 1. XYZ Specifications.................................... ...0100-5 <--------------------- "page footer style"

    Once again, your feedback is very much appreciated.

  16. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tables and Figures Macro (XP / Word 2003)

    I don't think the built-in TOC offers that option. This would imply that you'd have to construct it in code by parsing each and every paragraph in each and every document, which is a really horrible idea if you have to process hundreds of documents.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •