|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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.GoBackEnd Sub Last edited by Charles Kenyon; 10-24-2022 at 06:34 PM. Reason: format code |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
sample document from original poster
Here is a sample document that doesn't allow the macro to run properly.
df |
#4
|
|||
|
|||
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 |
#5
|
|||
|
|||
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 |
#6
|
|||
|
|||
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 |
#7
|
|||
|
|||
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 |
#8
|
|||
|
|||
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. |
#9
|
|||
|
|||
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. |
#10
|
||||
|
||||
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 |
#11
|
|||
|
|||
Thanks for help with the macros!
Thanks for help with the macros!
Both solutions work perfectly now. Much appreciated. David |
#12
|
|||
|
|||
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:
|
#13
|
|||
|
|||
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 |
#14
|
||||
|
||||
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 |
#15
|
|||
|
|||
Quote:
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? |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Keep text selected after paste | dabbler | Word | 14 | 01-11-2021 09:54 AM |
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 |
Find and Replace Selected Text or Macro for finding selected text | mrplastic | Word VBA | 4 | 12-20-2019 01:25 PM |
Help debugging a Macro that merges individual word docs into one document | vincenzo345 | Word VBA | 4 | 12-01-2017 11:25 AM |
Macro (debugging) | JACKsparrow | Word VBA | 1 | 03-09-2016 02:47 PM |