Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-27-2012, 01:01 AM
Rattykins Rattykins is offline VBA code to extract specific bookmarks from multiple word files Windows XP VBA code to extract specific bookmarks from multiple word files Office 2007
Novice
VBA code to extract specific bookmarks from multiple word files
 
Join Date: Jun 2012
Posts: 3
Rattykins is on a distinguished road
Question 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
Reply With Quote
  #2  
Old 06-27-2012, 04:02 AM
macropod's Avatar
macropod macropod is offline VBA code to extract specific bookmarks from multiple word files Windows 7 64bit VBA code to extract specific bookmarks from multiple word files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Simply replace the bookmark names in the 'ArrBkMks' variable with your own bookmark names, then run the macro. Point its browser to the target folder and let it run.

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]
Reply With Quote
  #3  
Old 06-27-2012, 06:54 PM
Rattykins Rattykins is offline VBA code to extract specific bookmarks from multiple word files Windows XP VBA code to extract specific bookmarks from multiple word files Office 2007
Novice
VBA code to extract specific bookmarks from multiple word files
 
Join Date: Jun 2012
Posts: 3
Rattykins is on a distinguished road
Default 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
Reply With Quote
  #4  
Old 06-27-2012, 07:30 PM
macropod's Avatar
macropod macropod is offline VBA code to extract specific bookmarks from multiple word files Windows 7 64bit VBA code to extract specific bookmarks from multiple word files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 06-27-2012, 10:02 PM
Rattykins Rattykins is offline VBA code to extract specific bookmarks from multiple word files Windows XP VBA code to extract specific bookmarks from multiple word files Office 2007
Novice
VBA code to extract specific bookmarks from multiple word files
 
Join Date: Jun 2012
Posts: 3
Rattykins is on a distinguished road
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code to extract specific bookmarks from multiple word files how to extract wav files from ppts t-4-2 PowerPoint 2 01-19-2012 02:24 AM
VBA code to extract specific bookmarks from multiple word files 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
VBA code to extract specific bookmarks from multiple word files 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft