RangeCopyMove.vb
- ''
- '' This code is part of Document Solutions for Word demos.
- '' Copyright (c) MESCIUS inc. All rights reserved.
- ''
- Imports System.IO
- Imports System.Drawing
- Imports System.Linq
- Imports GrapeCity.Documents.Word
-
- '' This sample shows how to copy or move ranges within a document,
- '' or between documents, using the RangeBase.CopyTo()/MoveTo() methods.
- ''
- '' The original SampleParagraphs.docx used in this sample can be
- '' seen by running the SampleParagraphs sample.
- Public Class RangeCopyMove
- Public Function CreateDocx() As GcWordDocument
- Const p1start = "This is the first paragraph of the original document"
- Const p2start = "This is the second paragraph of the original document"
- Const p3start = "This is the third paragraph of the original document"
- Const p4start = "This is the fourth paragraph of the original document"
-
- '' Load the sample DOCX file (see SampleParagraphs) into a GcWordDocument instance:
- Dim srcDoc = New GcWordDocument()
- srcDoc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"))
-
- '' Get the range of paragraphs to copy:
- Dim srcRng = srcDoc.Body.GetPersistentRange(srcDoc.Body.Paragraphs(2), srcDoc.Body.Paragraphs.Last)
-
- '' We use another document here to demonstrate that CopyTo()/MoveTo() methods
- '' can be used to copy/move content between documents:
- Dim doc = New GcWordDocument()
- srcRng.CopyTo(doc.Body, InsertLocation.Start)
-
- '' Find individual paragraphs inside the new document to manipulate:
- Dim p1 As Paragraph = Nothing, p2 As Paragraph = Nothing, p3 As Paragraph = Nothing, p4 As Paragraph = Nothing
- For Each p In doc.Body.Paragraphs
- Dim t = p.GetRange().Text
- If t.StartsWith(p1start) Then
- p1 = p
- ElseIf t.StartsWith(p2start) Then
- p2 = p
- ElseIf t.StartsWith(p3start) Then
- p3 = p
- ElseIf t.StartsWith(p4start) Then
- p4 = p
- End If
- Next
- If p1 Is Nothing OrElse p2 Is Nothing OrElse p3 Is Nothing OrElse p4 Is Nothing Then
- Throw New Exception("Unexpected: could not find paragraphs.")
- End If
-
- '' Move first paragraph to the end of the document with default formatting option
- '' (preserve formatting):
- p1.GetRange().MoveTo(doc.Body, InsertLocation.End)
-
- '' Move second paragraph to the end of the document, clearing formatting:
- p2.GetRange().MoveTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.Clear)
-
- '' Copy third paragraph to the end of the document, clearing formatting:
- p3.GetRange().CopyTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.Clear)
-
- '' Add a note at the end of the document:
- doc.Body.Sections.Last.GetRange().Paragraphs.Add($"Created by DsWord on {Util.TimeNow():R}.")
-
- '' Done:
- Return doc
- End Function
- End Class
-