RangeCopyMove.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Linq
  8. Imports GrapeCity.Documents.Word
  9.  
  10. '' This sample shows how to copy or move ranges within a document,
  11. '' or between documents, using the RangeBase.CopyTo()/MoveTo() methods.
  12. ''
  13. '' The original SampleParagraphs.docx used in this sample can be
  14. '' seen by running the SampleParagraphs sample.
  15. Public Class RangeCopyMove
  16. Public Function CreateDocx() As GcWordDocument
  17. Const p1start = "This is the first paragraph of the original document"
  18. Const p2start = "This is the second paragraph of the original document"
  19. Const p3start = "This is the third paragraph of the original document"
  20. Const p4start = "This is the fourth paragraph of the original document"
  21.  
  22. '' Load the sample DOCX file (see SampleParagraphs) into a GcWordDocument instance:
  23. Dim srcDoc = New GcWordDocument()
  24. srcDoc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"))
  25.  
  26. '' Get the range of paragraphs to copy:
  27. Dim srcRng = srcDoc.Body.GetPersistentRange(srcDoc.Body.Paragraphs(2), srcDoc.Body.Paragraphs.Last)
  28.  
  29. '' We use another document here to demonstrate that CopyTo()/MoveTo() methods
  30. '' can be used to copy/move content between documents:
  31. Dim doc = New GcWordDocument()
  32. srcRng.CopyTo(doc.Body, InsertLocation.Start)
  33.  
  34. '' Find individual paragraphs inside the new document to manipulate:
  35. Dim p1 As Paragraph = Nothing, p2 As Paragraph = Nothing, p3 As Paragraph = Nothing, p4 As Paragraph = Nothing
  36. For Each p In doc.Body.Paragraphs
  37. Dim t = p.GetRange().Text
  38. If t.StartsWith(p1start) Then
  39. p1 = p
  40. ElseIf t.StartsWith(p2start) Then
  41. p2 = p
  42. ElseIf t.StartsWith(p3start) Then
  43. p3 = p
  44. ElseIf t.StartsWith(p4start) Then
  45. p4 = p
  46. End If
  47. Next
  48. If p1 Is Nothing OrElse p2 Is Nothing OrElse p3 Is Nothing OrElse p4 Is Nothing Then
  49. Throw New Exception("Unexpected: could not find paragraphs.")
  50. End If
  51.  
  52. '' Move first paragraph to the end of the document with default formatting option
  53. '' (preserve formatting):
  54. p1.GetRange().MoveTo(doc.Body, InsertLocation.End)
  55.  
  56. '' Move second paragraph to the end of the document, clearing formatting:
  57. p2.GetRange().MoveTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.Clear)
  58.  
  59. '' Copy third paragraph to the end of the document, clearing formatting:
  60. p3.GetRange().CopyTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.Clear)
  61.  
  62. '' Add a note at the end of the document:
  63. doc.Body.Sections.Last.GetRange().Paragraphs.Add($"Created by DsWord on {Util.TimeNow():R}.")
  64.  
  65. '' Done:
  66. Return doc
  67. End Function
  68. End Class
  69.