如何将内容从源文档复制到新文档中的特定行/页?

huangapple go评论77阅读模式
英文:

How to copy content from a source doc to a specific line/page in a new doc?

问题

  1. 源内容被复制到新文档的第一行而不是后续行的情况下,您尝试的代码似乎没有达到预期的效果。这是因为在使用docNew.Range.formattedtext = docSource.Range.formattedtext时,它将源内容直接复制到新文档的范围中,这可能会覆盖掉第一行。要将源内容复制到新文档的后续行,您可以尝试以下代码:
' 在新文档中插入一个回车
docNew.Range.InsertAfter vbCr
' 然后将源内容复制到新文档的当前位置
docNew.Range.Collapse Direction:=wdCollapseEnd
docNew.Range.FormattedText = docSource.Range.FormattedText
  1. 源内容被复制到新页面的情况下,您尝试的代码也没有按预期工作。这是因为插入分页符后,您还需要将范围折叠到下一页。您可以尝试以下代码:
' 复制源内容到新文档
docNew.Range.FormattedText = docSource.Range.FormattedText
' 插入分页符
docNew.Range.InsertBreak Type:=wdPageBreak
' 折叠到下一页
docNew.Range.Collapse Direction:=wdCollapseEnd

这些代码应该能够实现您想要的两种情况。如果仍然存在问题,请确保文档的格式设置没有导致不良影响。

英文:

The following code copies content over from a source doc to a new doc. However, it copies content to the first line of the new doc?

Sub S1005A_CreateOpenClose()
    Const strSOURCE As String = "C:path\to\file\Report.docx"
    Dim docNew As Document
    Dim docSource As Document
    Dim blnOpen As Boolean
    
    ' Create new document
    Set docNew = Documents.Add
    
    ' Open source document if not already open
    blnOpen = S1005B_fncTestDocumentOpen(strSOURCE)
    If blnOpen = True Then
        Set docSource = Documents(strSOURCE)
    Else
        Set docSource = Documents.Open(FileName:=strSOURCE, ReadOnly:=True)
    End If
    
    ' Copy second paragraph
    docNew.Range.formattedtext = docSource.Paragraphs(1).Range.formattedtext
    
    
    ' Close source document if necessary
    If blnOpen = False Then
        docSource.Close SaveChanges:=False
    End If
End Sub

Function S1005B_fncTestDocumentOpen(strName As String) As Boolean
    Dim docEach As Document
    Dim blnOpen As Boolean
    
    blnOpen = False
    For Each docEach In Documents
        If docEach.Name = strName Then
            blnOpen = True
            Exit For
        End If
    Next docEach
    
    If blnOpen = True Then
        S1005B_fncTestDocumentOpen = True
    Else
        S1005B_fncTestDocumentOpen = False
    End If
End Function

Now I would like to have the following two situations:

  1. the source content is copied not on the first line of the new doc, but on a subsequent line.
  2. the source content is copied over to a new page

I tried to accomplish the first using the following code, but it doesn't work. I see the carriage return momentarily, but then the content shows on the first line.

docNew.Range.InsertBefore Text:="" & vbCr
docNew.Range.Collapse Direction:=wdCollapseEnd
docNew.Range.formattedtext = docSource.Range.formattedtext

I tried to accomplish the second using the following code, but it doesn't work. I see a page break momentarily, but the again as the code completes, all i see is the content on the first line of the first page:

docNew.Range.formattedtext = docSource.Range.formattedtext
docNew.Range.Collapse Direction:=wdCollapseEnd
docNew.Range.InsertBreak Type:=wdPageBreak

Can you please guide me where I am going wrong here.

答案1

得分: 1

  • 1
只需使用 [Range](https://learn.microsoft.com/office/vba/api/word.range) 对象来控制您想要的内容:

```vba
Dim rng As Range
Set rng = docNew.Paragraphs(1).Range
'docNew.Activate
With rng
    .InsertParagraphAfter
    .SetRange .Paragraphs(2).Range.Start, .Paragraphs(2).Range.End
    .FormattedText = docSource.Range.FormattedText
End With

介绍:

rem 这可能会折叠范围
docNew.Range.InsertBefore Text:="" & vbCr
rem 如果折叠范围,那么您将无法再对其进行操作。
docNew.Range.Collapse Direction:=wdCollapseEnd
rem 这个 Range 对象将成为 docNew 的范围
docNew.Range.formattedtext = docSource.Range.formattedtext

- 2
```vba
Dim rng As Range, endRng As Long
Set rng = docNew.Range
'docNew.Activate
With rng
    endRng = .End
    .InsertBreak Type:=wdPageBreak
    .SetRange endRng, .End
    .FormattedText = docSource.Range.FormattedText
End With    

介绍:

rem 这也是 docNew 的范围,而不是您想要的范围
docNew.Range.formattedtext = docSource.Range.formattedtext
rem 哇,您又折叠了范围,那么您将无法再通过 Range 对象进行任何操作
docNew.Range.Collapse Direction:=wdCollapseEnd
rem 仍然是 docNew 的范围,而不是新页面的范围,而且范围可能已经折叠了。所以您失去了对它们的控制。就是这样!
docNew.Range.InsertBreak Type:=wdPageBreak

<details>
<summary>英文:</summary>

Just use [Range](https://learn.microsoft.com/office/vba/api/word.range) object to control what you want:

- 1
```vba
    Dim rng As Range
    Set rng = docNew.Paragraphs(1).Range
    &#39;docNew.Activate
    With rng
        .InsertParagraphAfter
        .SetRange .Paragraphs(2).Range.Start, .Paragraphs(2).Range.End
        .FormattedText = docSource.Range.FormattedText
    End With

introduction:

rem this maybe collapse the range
docNew.Range.InsertBefore Text:=&quot;&quot; &amp; vbCr
rem if you Collapse the range then you can&#39;t operate by it anymore.
docNew.Range.Collapse Direction:=wdCollapseEnd
rem this Range object will be the docNem&#39;s range
docNew.Range.formattedtext = docSource.Range.formattedtext
  • 2
    Dim rng As Range, endRng As Long
    Set rng = docNew.Range
&#39;    docNew.Activate
    With rng
        endRng = .End
        .InsertBreak Type:=wdPageBreak
        .SetRange endRng, .End
        .FormattedText = docSource.Range.FormattedText
    End With    

Introduction:

rem this is also the docNew&#39;s range, not the one you want to
docNew.Range.formattedtext = docSource.Range.formattedtext
rem woo, you collapse the range again then you can&#39;t do anything by Range object 
docNew.Range.Collapse Direction:=wdCollapseEnd
rem Still this is the docNew&#39;s range, not the new page&#39;s range, and maybe the range is already collapsed. So you are out of control of these.That&#39;s it!
docNew.Range.InsertBreak Type:=wdPageBreak

huangapple
  • 本文由 发表于 2023年6月9日 10:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定