繁体   English   中英

以交替方式将两个 Word 文档中的段落复制/粘贴到不同的文档中(学习外语)

[英]Copy/paste paragraphs in alternating manner from two Word documents into a different document (to learn a foreign language)

我有两份不同语言的文档(相同的段落数量和格式)。 我想从 2 创建第三个文件,段落一个接一个地交替(学习外语)。 这些文件也有表格。 我已经尝试使用下面的代码,我从这里获得( 一个接一个地复制/粘贴两个 Word 文档中的后续段落(学习一门外语) ),但它在运行时错误“5251”的表上失败:这不是行尾的有效操作。

我怎样才能让它也贯穿表格,交替段落?

Sub AddSecondLanguage()
Application.ScreenUpdating = False
Dim DocA As Document, DocB As Document, Rng As Range, i As Long
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  .Title = "Select the source document containing the primary language."
  .InitialFileName = "C:\Users\" & Environ("Username") & "\Documents\"
  .AllowMultiSelect = False
  If .Show = -1 Then
    Set DocA = Documents.Open(.SelectedItems(1), ReadOnly:=True, AddToRecentFiles:=False)
  Else
    MsgBox "No primary language file selected. Exiting.", vbExclamation: Exit Sub
  End If
End With
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  .Title = "Select the source document containing the secondary language."
  .InitialFileName = DocA.Path & "\"
  .AllowMultiSelect = False
  If .Show = -1 Then
    Set DocB = Documents.Open(.SelectedItems(1), ReadOnly:=False, AddToRecentFiles:=True)
  Else
    MsgBox "No secondary language file selected. Exiting.", vbExclamation
    DocA.Close SaveChanges:=False: Set DocA = Nothing: Exit Sub
  End If
End With
With DocB
  For i = .Paragraphs.Count To 1 Step -1
    Set Rng = .Paragraphs(i).Range
    Rng.Collapse wdCollapseStart
    Rng.FormattedText = DocA.Paragraphs(i).Range.FormattedText
  Next
    .SaveAs2 FileName:=Split(DocA.FullName, ".doc")(0) & "-Combined.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
End With
DocA.Close SaveChanges:=False
Set DocA = Nothing: Set DocB = Nothing
Application.ScreenUpdating = True
End Sub

尝试改变:

Dim DocA As Document, DocB As Document, Rng As Range, i As Long

到:

Dim DocA As Document, DocB As Document, RngSrc As Range, RngTgt As Range, i As Long

并改变:

  For i = .Paragraphs.Count To 1 Step -1
    Set Rng = .Paragraphs(i).Range
    Rng.Collapse wdCollapseStart
    Rng.FormattedText = DocA.Paragraphs(i).Range.FormattedText
  Next

到:

  For i = .Paragraphs.Count To 1 Step -1
    Set RngTgt = .Paragraphs(i).Range
    RngTgt.Collapse wdCollapseStart
    Set RngSrc = DocA.Paragraphs(i).Range
    If RngSrc.Information(wdWithInTable) = True Then
      If RngSrc.End <> RngSrc.Rows(1).Range.End Then
        If RngSrc.End = RngSrc.Cells(1).Range.End Then RngSrc.InsertAfter vbCr
        RngTgt.FormattedText = RngSrc.FormattedText
      End If
    Else
      RngTgt.FormattedText = RngSrc.FormattedText
    End If
  Next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM