Results 1 to 2 of 2

Thread: Excel 2007 - Scan .docx files for TOC data

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    1

    Question Excel 2007 - Scan .docx files for TOC data

    Hi,

    My background is with databases not VBA so apologies if this seems simple.

    I have a folder

    \\data\

    it has 30+ subdirectories called
    \\data\TestGroupxx
    where xx is number between 1 and 34


    in each of these subdirectories I have approx 450 .docx files, each has a Table of Contents "TOC".

    I need to put an excel spreadsheet into \\data\ that has a macro which
    1) Scans for subdirectories
    2) For each directory create a new worksheet in the workbook
    3) Scan each directory looking for .docx files
    4) For each file retrieve the TOC
    5) For each TOC line put an entry into the worksheet
    Column A = Document Name ( eg Primary Compound Analysis.Docx)
    Column B = TOC Level ( eg 3.1.2)
    Column C = TOC Text ( eg Initial analysis of Carbon compound )
    Column D = TOC Page Number ( eg 28 )

    Does anybody have any code I could leverage, sample urls that might give me a clue ? I had a look on this site and stack overflow, but the code seems orders of magnitude beyond me

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,528

    Re: Excel 2007 - Scan .docx files for TOC data

    do you have one level of sub directories, or do sub directories have sub directories?

    i have not worked with TOCs, but it looks straight forward to do

    Code:
    set wrdapp = Createobject("word.application")
    for fold = 1 to 34
      set newsht = thisworkbook.sheets.add
      newsht.name = "Testgroup" & fold
      rw = 1
      fname = dir("\\data\testgroup" & fold & "\*.docx)
      do while len(fname) > 0
         set doc = wrdapp.documents.open("\\data\testgroup" & fold & "\" & fname)
         newsht.cells(rw, 1) = doc.name
               ' do stuff here to get TOC information from doc
         rw = rw + 1
         doc.close false
         fname = dir
      loop
    next
    i do not have any documents with toc information, so if you want help with that, you will have to post a sample document (saved as .doc) for me to work with
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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