2015-12-01 43 views
0

我有一个源和目标Word 2013文档。每个文档都有多个分节符,每个分节都有非常特殊的页脚,我不能打扰。我需要复制源文档中某个部分的内容(没有分节符),并将这些内容粘贴到目标文档的某个部分 - 例如,将源节3的文本复制到目标节5中。将部分内容复制到另一部分,而不会影响分节符?

问题是,当我复制源节时,该复制命令还包含源文档中的分节符字符。因此,当我将它粘贴到目标文档时,它会吹走该目标节的中断字符(或者如果该目标节是文档中的最后一个,并且因此在其后没有分节符字符,则添加新节)。

有Word中的方式,用VBA宏,只是从源文件给定段的原始内容复制不复制该节的分节符并将其粘贴到不同的文档没有吹走的是目的地区段的分节符

我已经尝试了所有种类的变化是这样的:

source.Sections(3).Range.Select 
source.Sections(3).Range.Copy 
dest.Sections(5).Range.Select 
dest.Sections(5).Range.Paste 

但贴线干扰目标文档的分节符。我也曾尝试从源文档减少选择长度(之前,我把它复制)一个字符,希望能排除分节符:

source.Sections(3).Range.Select 
source.ActiveWindow.Selection.MoveEnd Unit:=wdCharacter, Count:= -1 ' (I also tried -2, -3, etc) 
source.Sections(3).Range.Copy 
dest.Sections(5).Range.Select 
dest.ActiveWindow.Selection.MoveEnd Unit:=wdCharacter, Count:= -1 ' (I also tried -2, -3, etc) 
dest.Sections(5).Range.Paste 

这些削减在选择减少部分的实际文本,但似乎不排除分节符,我认为是在选择范围内?

+0

Deduplicator,如果我搜索了很久在这个网站上为这个问题,并找不到它。你介意告诉我这个重复的帖子在哪里? – perlhacker9876

回答

1

谢谢辛迪!你的建议让我到了需要的地方。你的代码需要调整一下。你把rngSec变成了Word.Section,但它抱怨了;我想你的意思是Word.Range,不是吗?而没有做一个rng.select,复制线抱怨没有选择文本。

这里是代码,以章节的内容从一个文档,并把它们纳入相反的顺序在不同的文档 - 在不影响任何分节符:

Option Explicit 

Sub switch_sections() 

Dim SourceDoc As Document, DestDoc As Document 
Dim i As Integer 
Dim has_section_break As Boolean 

Set SourceDoc = Application.Documents("source.docx") 
Set DestDoc = Application.Documents("destination.docx") 

Dim SrcRng As Range ' Word.Section 
Dim DestRng As Range ' Word.Section 

For i = 1 To SourceDoc.Sections.Count 
    With SourceDoc.Sections(i).Range.Find 
     ' Check for a section break. Put this find first, else it 
     ' screws up the selection we will do below. 
     .Text = "^b" 
     .Forward = True 
     .Wrap = wdFindStop 
     .Format = False 
     .Execute 
     If .Found Then 
      has_section_break = True 
     End If 
    End With 

    Set SrcRng = SourceDoc.Sections(i).Range 
    SrcRng.Select 
    If has_section_break Then SrcRng.MoveEnd wdCharacter, -1 
    SrcRng.Copy  ' Copy all but section break 

    With DestDoc.Sections(DestDoc.Sections.Count - (i - 1)).Range.Find 
     ' Check for a section break. Put this find first, else it 
     ' screws up the selection we will do below. 
     .Text = "^b" 
     .Forward = True 
     .Wrap = wdFindStop 
     .Format = False 
     .Execute 
     If .Found Then 
      has_section_break = True 
     End If 
    End With 
    Set DestRng = DestDoc.Sections(DestDoc.Sections.Count - (i - 1)).Range 
    DestRng.Select 
    If has_section_break Then DestRng.MoveEnd wdCharacter, -1 

    DestRng.Paste ' Replace all but the section break 
    Next 
End Sub 
1

您的代码存在的问题是您不会复制移动结束位置的内容。更改选择不会影响范围。

它的betterto与Range对象直接工作比选择。 MoveEnd方法应该适用于此。尝试像这样

Dim rngSec as Word.Range 
Set rngSec = source.Sections(3).Range 
rngSec.MoveEnd wdCharacter, -1 
rngSec.Copy 
+0

谢谢辛迪!你的建议让我到了我需要的地方!您的代码需要稍微调整一下,但这里是最终代码,将一个部分的内容从一个文档切换到另一个文档,而不会影响分节符: – perlhacker9876

+0

很高兴帮助:-)正如我看到您是Stack溢出:习惯上投票有用的评论和答案,并通过单击投票箭头下的复选标记来标记答案。 –

+0

我试图将它投票,但我想我没有15的声望 - 对不起! – perlhacker9876

相关问题