Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through Directory using VBA 2

Status
Not open for further replies.

moontho

Technical User
Jun 4, 2003
85
GB
Hi. I hope someone can help.

I'm using Word XP and in VBA I am wanting to loop though a whole directory of documents, and copy a selection of text from each which I've already defined using bookmarks then have that text pasted in an existing large document!.

I'm ok with getting the bookmarks etc, but i'm not sure about looping through a directory.

If anyone knows a snippet of code to get me on my way, i'd be very grateful.

Many thanks

thom
 
Take a look at the Dir function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Look at my FAQ, faq68-5300, on using Form Fields in Word. Code sample E shows how to loop through a folder, picking up data from files. It uses the Dir function mentioned by PHV.

It could easily be modified to get your bookmarks.

Gerry
 
sorry about this but I can't really seem to be able to get it to work!

I've got my main document open (Main.doc) that I want the bookmark from each document in a directory open, and when i run the code (modified) it closes my Main.doc.

I'm sure i've made a complete hash of modifying the code but here's what i've done.

Sub GetAv()
Dim aDoc
Dim ThisDoc As Document

aDoc = Dir("L:\Mydocs\*.DOC")
On Error Resume Next

Do While aDoc <> ""
Applications.Documents.Open FileName:=aDoc
Set ThisDoc = ActiveDocument

If ThisDoc.Bookmarks.Exists("Combine") Then
Selection.GoTo What:=wdGoToBookmark, Name:="Combine"
Selection.Copy
End If

ThisDoc.Close
Set ThisDoc = Nothing
aDoc = Dir()

Selection.PasteAndFormat (wdPasteDefault)

Loop
End Sub

Any pointers of where i'm going wrong would be brilliant

thanks

thom
 
First off. Rename your code. You are not getting an average (as the original did).

OK, you need to return to your original document. Make your main.doc an explicitly named object. Here it is modified.

Code:
Sub GetBookmarks()
Dim myMain as Document
Dim aDoc
Dim ThisDoc As Document

[COLOR=red]' explicitly make main.doc an object[/color red]
Set myMain = ActiveDocument

aDoc = Dir("L:\Mydocs\*.DOC")
On Error Resume Next

Do While aDoc <> ""
    Applications.Documents.Open FileName:=aDoc
' this sets current open file as object
    Set ThisDoc = ActiveDocument
    
    If ThisDoc.Bookmarks.Exists("Combine") Then
        Selection.GoTo What:=wdGoToBookmark, Name:="Combine"
        Selection.Copy
    End If
[COLOR=red]' returns to the original document
' and THEN pastes[/color red]
 myMain.Activate
 Selection.PasteAndFormat (wdPasteDefault)
' close and release open file
   ThisDoc.Close
    Set ThisDoc = Nothing
    aDoc = Dir()
Loop
' release the myMain object
Set myMain = Nothing
End Sub


Gerry
 
Thanks for this Gerry, but sorry to be a total pain.

I've updated the aDoc = Dir("") with the location of the files with the bookmarks to be copied, but when i F8 through the code, it's not opening the documents referenced in aDoc.

I've tried the code in the Normal.dot as well as in it's own template. I'm sure it's me doing something wrong, but if you have any ideas as to what, i'd be very gratefull.

Thanks

Thom
 
OK, you are not being a pain, at all. We all have to learn, starting somewhere.

Gerry
 
Have you tried to replace this:
Applications.Documents.Open FileName:=aDoc
By this ?
Applications.Documents.Open FileName:="L:\Mydocs\" & aDoc
Just a though: Applications or Application ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks Gerry.... and PH.

I think i've got it sorted now. Along the lines of PH's suggestion, at the beginning of the routine I did a ChangeFileOpenDirectory to the required directory and that got the whole thing going. Works a dream.

Also been having great fun with Ifs and Elses that Gerry helped me with a couple of weeks ago.

A star.

Thanks

Thom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top