#1
|
|||
|
|||
VBA code to extract specific bookmarks from multiple word files
Good afternoon everyone. VBA beginner looking to accomplish a task quite a beyond his skill level. I would very much appreciate a push in the right direction (though I will not turn down bits of code, if you are so inclined to produce <grin>). I am looking to create the following: A VBA macro that copies specific bookmarks from within a word file to another word file. The bookmarks are (for the most part) of the two bracketed variety (completely enclosing the target). Ideally, the macro could repeat this task for every word file within a given directory, producing a single "output"" word file that looks somthing like this: -- Bookmark 1 from doc A Bookmark 2 from doc A Bookmark 1 from doc B Bookmark 2 from doc B Bookmark 1 from doc C Bookmark 2 from doc C --- While I can generally muddle through various goto commands in VBA, I remain fairly inexperienced with if, then loops as well as how to work with directories full of files. A gentle nudge (or terrific push) in the right direction would be most appreciated. Best, Justin |
#2
|
||||
|
||||
You could use code like:
Code:
Sub GetBookmarks() Application.ScreenUpdating = False Dim strFolder As String, strFile As String, wdDoc As Document Dim ArrBkMks As String, i As Long, StrBkMk As String, StrOut As String ArrBkMks = "Bookmark1,Bookmark2,Bookmark3" strFolder = GetFolder If strFolder = "" Then Exit Sub strFile = Dir(strFolder & "\*.doc", vbNormal) While strFile <> "" Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False) With wdDoc StrOut = StrOut & .Name For i = 0 To UBound(Split(ArrBkMks, ",")) StrBkMk = Split(ArrBkMks, ",")(i) StrOut = StrOut & vbTab & StrBkMk & ": " If .Bookmarks.Exists(StrBkMk) Then StrOut = StrOut & .Bookmarks(StrBkMk).Range.Text End If Next i End With StrOut = StrOut & vbCr wdDoc.Close SaveChanges:=False strFile = Dir() Wend Set wdDoc = Nothing ActiveDocument.Range.InsertAfter StrOut Application.ScreenUpdating = True End Sub Function GetFolder() As String Dim oFolder As Object GetFolder = "" Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0) If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path Set oFolder = Nothing End Function You'll get a list of the documents, the bookmarks and their contents exported to the active document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Word of thanks
Macropod,
First and foremost, my sincerest thanks. Despite kidding about the code-writing (ok, half-kidding <grin>), your willingness to post that amount of code is most generous. As for my part, my ultimate goal is to be the creator, rather than simply the consumer, of macros like that. Provided I have some spare time, I'll dissect the code as best I can to learn how it works. A follow-up question, more in regards to macro-coding concepts rather than actual code. My ultimate goal is to use this macro to auto-generate a document for human consumption. Accordingly, I'm going to have to format the output. Given my hunch as to how the macro works, would this better be accomplished by a separate task (an additional macro), or could such a step be incorporated into a single-step, more sophisticated macro? Again, many thanks, Rattykins |
#4
|
||||
|
||||
Hi Rattykins,
There are numerous ways the formatting could be done. The code I posted simply gathers up all the data, looping through all the source documents to do so, and only once that's done does it output the results to the active document. It would be just as feasible to output the results as each source file is processed - either on a whole-of-file basis or on a per item (filename/bookmark, etc) basis. This gives a great deal of flexibility for formatting considerations - the method to be employed all depends on a combination of what you're comfortable with and what the required end result is. For example, suppose you wanted all the filenames in bold. One way would be to output those as the loop progresses, apply the bold attribute, then output the bookmark data without the bold attribute. Another would be to output all the data as is now done, convert it to a table, bold whatever's in the first column, then convert the table back to text again. Just as effective, and possibly faster. Yet another would be update the output document at the end of each file's processing, then bold whatever appears on the new line up to the first tab. As you can see, with just this one example, there's at least three ways of handling the 'problem'. Any/all of these could be handled by adding code to the existing macro or by building a separate macro that the existing one calls.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Macropod,
You are absolutely stellar. I'll attempt to sit down and dissect the code, perhaps even incorporating a few changes to get the output looking like a proper document. Expect desperate cries for help in the near future <grin>. Again, thank you so much. Rattykins |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
how to extract wav files from ppts | t-4-2 | PowerPoint | 2 | 01-19-2012 02:24 AM |
how to transfer multiple excel cells into specific place in word document by formulas | anton | Excel | 1 | 12-20-2011 03:56 AM |
Extract Numbers from Zip Code | Karen615 | Excel | 3 | 09-21-2011 06:54 AM |
convert multiple csv files to multiple excel files | mit | Excel | 1 | 06-14-2011 10:15 AM |
Links between files to bookmarks in PDF | spud098 | Word | 0 | 07-14-2010 01:11 AM |