将多个文件中的所有内容控件合并为一个

时间:2015-11-19 06:29:40

标签: vba ms-access ms-word word-contentcontrol

我正在研究vba项目。要求是将多个word文档内容合并为一个单个word文档。它由ContentControl标签完成。我已经创建了两个具有唯一内容控制标记ID的word文档。现在我需要构建逻辑以将其内容合并为一个。

  Set WordDoc = WordApp.Documents.Open(FileName)

  With WordApp.ActiveDocument
          For i = 1 To .ContentControls.Count
                Select Case .ContentControls(i).Tag
                    Case "cc1": strEnding = "st"  -- word content including format and style
                    Case "cc2": strEnding = "nd"
                    Case "cc3": strEnding = "rd"
                    Case Else: strEnding = "th"
                End Select
          Next
        End With

现在我想将所有数据(包括格式和样式,如果有表格)合并到一个文档中。

请建议我如何实现这一目标。

2 个答案:

答案 0 :(得分:1)

Sub Foo()
Dim i As Long
Dim MyName As String, MyPath As String
Application.ScreenUpdating = False
Documents.Add

MyPath = "C:\your_path_here\" ' <= change this as necessary

MyName = Dir$(MyPath & "*.doc") ' not *.* if you just want doc files

Do While MyName <> ""
If InStr(MyName, "~") = 0 Then
Selection.InsertFile _
FileName:="""" & MyPath & MyName & """", _
ConfirmConversions:=False, Link:=False, _
Attachment:=False
Selection.InsertBreak Type:=wdPageBreak
End If

MyName = Dir ' gets the next doc file in the directory
Loop

End Sub

答案 1 :(得分:0)

一般来说,它应该使用Range.FormattedText作为等式的两边。获取一个文档中的目标范围,并将单个内容控件的范围的FormattedText分配给它。