Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-24-2022, 08:16 AM
dfuent1321 dfuent1321 is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Novice
Help with debugging a simple macro to cut and paste selected text to end of document
 
Join Date: Oct 2022
Posts: 7
dfuent1321 is on a distinguished road
Default Help with debugging a simple macro to cut and paste selected text to end of document

I've suddenly experienced an error when running a macro I made to cut a selection, then paste it at the end of a document, then return to the original spot. It's worked for a long time, but now I get an error when I try to run it in some documents, including any new ones I make. I've searched the forum for a solution, but can't find anything close enough to my problem.
Thanks in advance!
Code:
Sub MoveToOutTakes()
'
' MoveToOutTakes Macro
'
Selection.Cut Selection.EndKey Unit:=wdStory Selection.TypeParagraph Selection.Paste Application.GoBack Application.GoBack
End Sub


Last edited by Charles Kenyon; 10-24-2022 at 06:34 PM. Reason: format code
Reply With Quote
  #2  
Old 10-24-2022, 08:35 AM
Charles Kenyon Charles Kenyon is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 11 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

I do not see anything wrong with the code.
Could you perhaps attach a sample document where it will not run?
How to attach a screenshot or file in this forum.


You may want to look into using the debugging tools built into the editor.
VBA: How to Debug Code - Overview, Tools, Shortcut Keys
Reply With Quote
  #3  
Old 10-24-2022, 11:07 AM
dfuent1321 dfuent1321 is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Novice
Help with debugging a simple macro to cut and paste selected text to end of document
 
Join Date: Oct 2022
Posts: 7
dfuent1321 is on a distinguished road
Default sample document from original poster

Here is a sample document that doesn't allow the macro to run properly.
df
Attached Files
File Type: docx dfuent1321-sample-document.docx (23.4 KB, 5 views)
Reply With Quote
  #4  
Old 10-24-2022, 06:36 PM
Charles Kenyon Charles Kenyon is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 11 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

I am having no trouble with your document running the macro. No errors are generated and the selected text gets moved. Are you perhaps not selecting anything first.

I do not especially care for the insertion of the blank paragraphs. They make documents more complex and harder to edit.
2.1 Why you should not press Enter at the end of every line
Reply With Quote
  #5  
Old 10-25-2022, 11:18 AM
dfuent1321 dfuent1321 is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Novice
Help with debugging a simple macro to cut and paste selected text to end of document
 
Join Date: Oct 2022
Posts: 7
dfuent1321 is on a distinguished road
Default macro trouble

Thanks for your attention C.K. I can't say what make the trouble occur in some Word Docs on my computer but not yours. I'm not a whiz at such things.
As for the extra paragraphs, this macro is set up to cut blocks of text (and sometimes also graphics) that I don't want to delete permanently and deposit them into a section at the end of my documents I call "Out Takes." I've found that if I don't add extra returns, the text collects into one big jumble.

Is there anything else I can try to get this macro (and a couple other similar ones I depend on) to work?
Thanks!
df
Reply With Quote
  #6  
Old 10-27-2022, 05:59 PM
BrianHoard BrianHoard is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Hi,
I'm very new to VBA, but hopefully this can help.
In my code below, rather than using the clipboard, I'm storing your selection as formatted text. Writing that to the end of the document, then deleting the original selection. After the write to the end of the doc, I add a carriage return, (vbCr).

I also added a message in case nothing was selected.
I tested this with an image in the selection and it also worked fine.


Code:
Sub MoveToOutTakes()
  
  Dim docEnd As Range
  Set docEnd = ActiveDocument.Range
  docEnd.Collapse direction:=collapseEnd
  
  Dim sel As Range
  Set sel = Selection.FormattedText
  
  If (sel.Start = sel.End) Then
    Call MsgBox(Prompt:="Nothing selected.", Buttons:=vbInformation)
  End If
  
  docEnd.FormattedText = sel
  docEnd.InsertAfter (vbCr)
  sel.Delete

End Sub
Reply With Quote
  #7  
Old 10-28-2022, 06:41 AM
dfuent1321 dfuent1321 is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Novice
Help with debugging a simple macro to cut and paste selected text to end of document
 
Join Date: Oct 2022
Posts: 7
dfuent1321 is on a distinguished road
Default Macro Problem Solved!

Many Thanks, Brian!
Wow! This macro not only works, I can't see it working—bouncing to the end of the document and back again like the one I recorded does.
I wonder if I could beg your help fixing the cousin of the macro you rewrote for me? It, too, works in some documents, but not others. I wrote it to help reorder a sentence—moving a block from the middle to the beginning of the sentence and correcting the capitilazation. Like its cousin, I sometimes get an error message, caused because the macro chokes when asked to paste the text it cut.
Here's my code:

Sub MoveBlockToBeginningOfSentence()
'
' MoveBlockToBeginningOfSentence Macro
'
'
Selection.Cut
Application.Run MacroName:="Normal.NewMacros.SentenceLeft"
Application.Run MacroName:="Normal.NewMacros.CapitalizeNextLetter"
Selection.Paste
Application.Run MacroName:="Normal.NewMacros.SentenceLeft"
Application.Run MacroName:="Normal.NewMacros.CapitalizeNextLetter"
End Sub
Reply With Quote
  #8  
Old 10-28-2022, 06:15 PM
BrianHoard BrianHoard is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Cool, glad the first one helped. For this one, you're calling the following macros: Normal.NewMacros.SentenceLeft and Normal.NewMacros.CapitalizeNextLetter
If you post the contents of those, I could take a look.

And/Or, if you post an example document with a before/after of your starting point and what your goal is, that's also very helpful.
Reply With Quote
  #9  
Old 10-31-2022, 07:37 AM
dfuent1321 dfuent1321 is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Novice
Help with debugging a simple macro to cut and paste selected text to end of document
 
Join Date: Oct 2022
Posts: 7
dfuent1321 is on a distinguished road
Default text for missing macros

Hello Brian,
Here are the two macros called into play by the one that I posted last week. It’s worked for me for about 20 years now, but suddenly quit. Thanks in advance for your help!

Sub SentenceLeft()
'
' SentenceLeft Macro
'
'
Selection.MoveLeft Unit:=wdSentence, Count:=1
End Sub


Sub CapitalizeNextLetter()
'
' CapitalizeNextLetter Macro
'
'
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Range.Case = wdToggleCase
Selection.MoveLeft Unit:=wdCharacter, Count:=1
End Sub

I’ve attached an illustration of the desired effect.

By the way, I tried to find a way to get the following joke to work for this illustration, but was unsuccessful:
My wife: You need to do more chores around the house. Me: Can we change the subject? My wife: Ok. More chores around the house need to be done by you.
Attached Images
File Type: jpg continuous-motion.jpg (90.7 KB, 26 views)
Reply With Quote
  #10  
Old 10-31-2022, 03:48 PM
Guessed's Avatar
Guessed Guessed is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try this version of the macro - it doesn't require the secondary macros.
Code:
Sub MoveBlockToBeginningOfSentence()
  Dim aRngMove As Range, aRngStart As Range
  Set aRngMove = Selection.Range
  If aRngMove.Characters.Last <> " " Then 'ensure the selection includes a space at the end
    aRngMove.MoveEndUntil cset:=" "
    aRngMove.MoveEnd Unit:=wdCharacter, Count:=1
  End If
  Set aRngStart = aRngMove.Sentences(1)
  aRngStart.Characters(1).Case = wdLowerCase
  aRngStart.Collapse Direction:=wdCollapseStart
  aRngMove.Characters(1).Case = wdTitleWord
  aRngStart.FormattedText = aRngMove.FormattedText
  aRngMove.Delete
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #11  
Old 11-01-2022, 06:43 AM
dfuent1321 dfuent1321 is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Novice
Help with debugging a simple macro to cut and paste selected text to end of document
 
Join Date: Oct 2022
Posts: 7
dfuent1321 is on a distinguished road
Default Thanks for help with the macros!

Thanks for help with the macros!
Both solutions work perfectly now.
Much appreciated.
David
Reply With Quote
  #12  
Old 06-21-2023, 12:47 AM
teachwrite teachwrite is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 11 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Novice
 
Join Date: Jun 2023
Posts: 3
teachwrite is on a distinguished road
Default

I know its been a while since this post, but can someone suggest how to modify this macro so that the selected words are pasted at the bottom of a different document, rather than the same doc? (I want them pasted at the end of a style sheet). Thanks!

Quote:
Originally Posted by BrianHoard View Post
Hi,
I'm very new to VBA, but hopefully this can help.
In my code below, rather than using the clipboard, I'm storing your selection as formatted text. Writing that to the end of the document, then deleting the original selection. After the write to the end of the doc, I add a carriage return, (vbCr).

I also added a message in case nothing was selected.
I tested this with an image in the selection and it also worked fine.


Code:
Sub MoveToOutTakes()
  
  Dim docEnd As Range
  Set docEnd = ActiveDocument.Range
  docEnd.Collapse direction:=collapseEnd
  
  Dim sel As Range
  Set sel = Selection.FormattedText
  
  If (sel.Start = sel.End) Then
    Call MsgBox(Prompt:="Nothing selected.", Buttons:=vbInformation)
  End If
  
  docEnd.FormattedText = sel
  docEnd.InsertAfter (vbCr)
  sel.Delete

End Sub
Reply With Quote
  #13  
Old 06-21-2023, 08:41 AM
gmaxey gmaxey is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of 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

Brian,


This is just another abbreviated version of your code:

Code:
Sub MoveToOutTakes()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  oRng.Collapse wdCollapseEnd
  If Selection.Start = Selection.End Then
    MsgBox "Nothing selected", vbOKOnly
    Exit Sub
  End If
  oRng.FormattedText = Selection.FormattedText
  Selection.Range.Delete
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 06-21-2023, 06:21 PM
Guessed's Avatar
Guessed Guessed is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 10 Help with debugging a simple macro to cut and paste selected text to end of document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

teachwrite

Is the 'different' document to write to already open or does the code have to open it? If it is open, how will the code know which document it is? Putting a copy of the selected text somewhere else is easy but the code needs to know how to specify that document.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #15  
Old 06-21-2023, 09:30 PM
teachwrite teachwrite is offline Help with debugging a simple macro to cut and paste selected text to end of document Windows 11 Help with debugging a simple macro to cut and paste selected text to end of document Office 2021
Novice
 
Join Date: Jun 2023
Posts: 3
teachwrite is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
teachwrite

Is the 'different' document to write to already open or does the code have to open it? If it is open, how will the code know which document it is? Putting a copy of the selected text somewhere else is easy but the code needs to know how to specify that document.
I'd like the code to create a new document and save it within the same folder as the source document. The new doc should be named 'Style sheet'.

Then, that style sheet doc would stay open, and any subsequent times the macro is run, it would add the selected words to that style sheet doc.

Is that doable?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with debugging a simple macro to cut and paste selected text to end of document Keep text selected after paste dabbler Word 14 01-11-2021 09:54 AM
Help with debugging a simple macro to cut and paste selected text to end of document Copy selected text from one document and paste to new document in same position on the page gasparik Word VBA 1 05-11-2020 05:41 AM
Help with debugging a simple macro to cut and paste selected text to end of document Find and Replace Selected Text or Macro for finding selected text mrplastic Word VBA 4 12-20-2019 01:25 PM
Help with debugging a simple macro to cut and paste selected text to end of document Help debugging a Macro that merges individual word docs into one document vincenzo345 Word VBA 4 12-01-2017 11:25 AM
Help with debugging a simple macro to cut and paste selected text to end of document Macro (debugging) JACKsparrow Word VBA 1 03-09-2016 02:47 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:59 AM.


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