|
-
Aug 29th, 2012, 02:11 AM
#1
Thread Starter
New Member
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
-
Aug 29th, 2012, 06:10 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|