MS Word VBA to insert copied portion of a document below the current page

Hello,

I have a macro that copies a body of text and places it into a new section at the "bottom" of the current document.  (See SAMPLE CODE below.)

The macro is connected to a Macro Button that is part of a locked MS Word 2010 macro enabled template.

I am attempting to create a similar macro that will copy a body of text and place into a new section "directly below" the section that contains the macro button.  Can I modify the existing macro to do that?

I assume I need a way to have the macro set the insertion point below the macro button so that the page will paste in the proper location.

Any assistance will be appreciated.

Thanks

------------------------

SAMPLE CODE

Sub CreateNewProgressNotePage()

'Code below unlocks the original document
 If ActiveDocument.ProtectionType <> wdNoProtection Then
    ActiveDocument.Unprotect
 End If
 
'Code below selects the text between two Bookmarks in the document
 Dim oRng As Word.Range
 Set oRng = ActiveDocument.Range
    oRng.Start = ActiveDocument.Bookmarks("P_Start").Range.End
    oRng.End = ActiveDocument.Bookmarks("P_End").Range.Start
    oRng.Select
   
' Code below will Unhide the selection
With Selection.Font
        .Hidden = False
End With
   
' Code below copys the selected text
 Selection.Copy
 
' Code below will Hide the selection again
With Selection.Font
        .Hidden = True
End With

' Code below navigates to the end of the document and creates a section break
    Selection.EndKey Unit:=wdStory
    Selection.InsertBreak Type:=wdSectionBreakNextPage

 
' Code below pastes what was copied earlier
 Selection.PasteAndFormat (wdFormatOriginalFormatting)
 
' Code below relocks the original document
  ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
 
' Code below navigates to the End of the Document

    Selection.EndKey Unit:=wdStory
    Selection.MoveUp Unit:=wdScreen, Count:=1
 
 
End Sub

Answer
Answer

If you prefer to maintain the structure of your existing macro, replace these lines

' Code below navigates to the end of the document and creates a section break
    Selection.EndKey Unit:=wdStory
    Selection.InsertBreak Type:=wdSectionBreakNextPage

with these lines:

    ' Code below navigates to the end of the section containing the macro button
    ' and creates a section break
    Selection.Sections(1).Range.Select
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.InsertBreak Type:=wdSectionBreakNextPage

However, your macro can be made more efficient, as well as leaving the cursor position and the clipboard contents unchanged, by using only Range objects to do the manipulations:

Sub RevisedSample()
   Dim oRngFrom As Range
   Dim oRngTo As Range
   
   If ActiveDocument.ProtectionType <> wdNoProtection Then
      ActiveDocument.Unprotect
   End If
   
   ' Set a range to the source text (it can stay hidden)
   Set oRngFrom = ActiveDocument.Range
   oRngFrom.Start = ActiveDocument.Bookmarks("P_Start").Range.End
   oRngFrom.End = ActiveDocument.Bookmarks("P_End").Range.Start
   
   ' Set a second range to the end of the section containing the macro button
   Set oRngTo = Selection.Range.Sections(1).Range
   oRngTo.Collapse Direction:=wdCollapseEnd
   
   ' Assigning the FormattedText from one range to another
   ' has the same effect as copying and then pasting while maintaining
   ' the format. Then unhide the text at the destination.
   oRngTo.FormattedText = oRngFrom.FormattedText
   oRngTo.Font.Hidden = False
   
   ' Go to the end of the destination range and insert a break.
   oRngTo.Collapse Direction:=wdCollapseEnd
   oRngTo.InsertBreak Type:=wdSectionBreakNextPage
   
   ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 2,966 Applies to: