Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-12-2024, 11:48 AM
Trio78 Trio78 is offline Copy Current Section to a New Document Windows 11 Copy Current Section to a New Document Office 2021
Novice
Copy Current Section to a New Document
 
Join Date: Mar 2024
Posts: 3
Trio78 is on a distinguished road
Default Copy Current Section to a New Document

I have a document that is separated by section breaks. I want to have a macro that will take the current section (where my cursor is) and copy it into a new document.



I'm getting the Run-time error '4605' (This command is not available), which my web research suggests is due to using the .Copy and .PasteAndFormat functions. What I haven't been able to figure out is how to rewrite this without using those functions, however!

Code:
Sub CopyCurrentSectionToNewDoc()
  currentSection = Selection.Information(wdActiveEndSectionNumber)
  With ActiveDocument.Sections(currentSection)
    Set Rng = .Range
      With Rng
        .MoveEnd wdCharacter, -1
        .Copy
      End With
  End With
  
  Set NewDoc = Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName, Visible:=True)
    
  With NewDoc
    ' Paste contents into the output document, preserving the formatting
    .Range.PasteAndFormat (wdFormatOriginalFormatting)
  End With

End Sub
Reply With Quote
  #2  
Old 03-12-2024, 12:22 PM
gmaxey gmaxey is offline Copy Current Section to a New Document Windows 10 Copy Current Section to a New Document Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

With the exception of not declaring your variables, your code works fine here.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 03-12-2024, 12:52 PM
Italophile Italophile is offline Copy Current Section to a New Document Windows 11 Copy Current Section to a New Document Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

To avoid using the clipboard:

Code:
 Sub CopyCurrentSectionToNewDoc()
  Dim Rng as Range, NewDoc as Document, currentSection as Long

  currentSection = Selection.Information(wdActiveEndSectionNumber)
  Set Rng = ActiveDocument.Sections(currentSection).Range
  Rng.MoveEnd wdCharacter, -1
  
  Set NewDoc = Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName, Visible:=True)
  
  ' Transfer contents into the output document, preserving the formatting
  NewDoc.Range.FormattedText = Rng.FormattedText
 
End Sub
Reply With Quote
  #4  
Old 03-12-2024, 01:20 PM
Trio78 Trio78 is offline Copy Current Section to a New Document Windows 11 Copy Current Section to a New Document Office 2021
Novice
Copy Current Section to a New Document
 
Join Date: Mar 2024
Posts: 3
Trio78 is on a distinguished road
Default

Quote:
Originally Posted by Italophile View Post
To avoid using the clipboard:
Thanks, that's just what I was trying to figure out.
Reply With Quote
  #5  
Old 03-12-2024, 11:07 PM
gmayor's Avatar
gmayor gmayor is offline Copy Current Section to a New Document Windows 10 Copy Current Section to a New Document Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You don't even need to identify the section number e.g.
Code:
Sub CopyCurrentSectionToNewDoc()
Dim rng As Range
Dim NewDoc As Document
    Set rng = Selection.Sections(1).Range
    rng.End = rng.End - 1
    Set NewDoc = Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName, Visible:=True)
    NewDoc.Range.FormattedText = rng.FormattedText
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Current Section to a New Document Prevent header from using StyleRef x when x is not present in current section Firebody Word 8 11-12-2021 10:53 AM
Require VBA to copy value into current field of Internet Explorer trevorc Excel Programming 0 01-15-2019 03:17 PM
If Cell Blank Copy Data from Above Combine All to Current ChrisOK Excel Programming 2 12-18-2017 11:25 PM
Copy Current Section to a New Document Show current section in status bar Phil H Word 2 02-02-2016 01:45 PM
a macro that can copy data from copy.xls to our current excel macro.xls based on criteria: udhaya Excel Programming 1 11-12-2015 10:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:09 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft