以下是引用tingliu在2006-12-24 18:51:03的发言: 我要做的是形如附件的事情。文件名都有规律:如将txt0001.doc与ans0001.doc合并。
请参考: '* +++++++++++++++++++++++++++++ '* Created By SHOUROU@ExcelHome 2006-12-28 6:33:47 '仅测试于System: Windows NT Word: 11.0 Language: 2052 '№ 0119^The Code CopyIn [ThisDocument-ThisDocument]^' '* ----------------------------- Option Explicit
Sub DocMerge() Dim MyDialog As FileDialog, oItem As Variant, strA As String, strB As String Dim These() As String, Those() As String, myDoc As Document, myRange As Range Dim A As Integer, B As Integer, FullNm() As String, Fname As String strA = "ans" strB = "txt" '定义一个文件夹选取对话框 Set MyDialog = Application.FileDialog(msoFileDialogFilePicker) With MyDialog .Filters.Clear '清除所有文件筛选器中的项目 .Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件 .AllowMultiSelect = True '允许多项选择 If .Show = -1 Then '确定 If .SelectedItems.Count Mod 2 <> 0 Then MsgBox "不对称的文件数量,Word无法完成两两合作操作!", vbExclamation + vbInformation, "Excelhome" Exit Sub Else For Each oItem In .SelectedItems '在所有选取项目中循环 FullNm = Split(oItem, "\") Fname = FullNm(UBound(FullNm)) Fname = VBA.LCase(Left(Fname, 3)) If Fname = strA Then ReDim Preserve These(A) These(A) = oItem A = A + 1 ElseIf Fname = strB Then ReDim Preserve Those(B) Those(B) = oItem B = B + 1 End If Next End If End If End With |