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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merge multiple Word Documents using VBA 2

Status
Not open for further replies.

Cloonalt

Programmer
Jan 4, 2003
354
US
I have allow the user click a button to combine multiple Word documents into one, and then launch the final product. Any idea if this is possible?

(I'll know the names of the documents from a recordset)

Any help appreciated.
 
Thanks for the quick reply.

I open Word and create a new blank document

I know the name of the Word document (and where it SHOULD be) that I'm merging into the new blank document.

However, if that document isn't in the directory where I think it is or doesn't exist, I need to handle that error.

Thanks.
 
Ah, sorry. You can use Dir:
[tt]If Dir("c:\Docs\Doc1.doc") = "" Then
'File does not exist[/tt]
 
The documents all have a name that is five digits, with a three digit extension. The extension can be 001, 002, 003, etc.

For example, right now I using:

oapp.Selection.InsertFile "C:\" & Batch & "001.doc"

I need to grab the document with the highest extension. Think I can do this? Any ideas?

Thanks.
 
Code:
Function FindLastFile(File_Name As String, Search_Folder As String) As String

Dim rst As ADODB.Recordset

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(Search_Folder)
Set fls = folder.files

Set rst = New ADODB.Recordset
With rst
   .CursorLocation = adUseClient
   .CursorType = adOpenDynamic
   .LockType = adLockBatchOptimistic
   .Fields.Append "Name", adVarChar, 252
   .Open
   For Each fl In fls
      .AddNew 
      .Fields("Name") = fl.Name
   Next
   .UpdateBatch
   .Filter "Name = " & File_Name & "???.doc"
   .Sort = "Name"
   If .EOF And .BOF Then
      FindLastFile = "Not Found" ' Handle this error!
   Else
      .MoveLast
      FindLastFile = .Fields("Name")
   End If
   .Close
End With
Set rst = Nothing

End Function

....
myMaxFileName = FindLastFile(Batch, "c:\")
....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top