Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 06-21-2023, 10:36 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: 4,166
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 code
Code:
Option Explicit

Const sSS As String = "StyleSheet.docx"

Sub Send2StyleSheet()
  Dim sPath As String, docSS As Document, aRng As Range, rngTarget As Range
  Set aRng = Selection.Range
  If Len(aRng.Text) > 0 Then
    sPath = ActiveDocument.Path & Application.PathSeparator
    Set docSS = GetSS(sPath)
    Set rngTarget = docSS.Range
    rngTarget.Collapse Direction:=wdCollapseEnd
    rngTarget.Text = vbCr & aRng.Text
  End If
End Sub

Function GetSS(sPath As String) As Document
  Dim aDoc As Document
  For Each aDoc In Documents
    If aDoc.FullName = sPath & sSS Then
      Set GetSS = aDoc
      Exit For
    End If
  Next aDoc
  If GetSS Is Nothing Then
    Set GetSS = Documents.Add()
    GetSS.SaveAs FileName:=sPath & sSS
  End If
End Function

__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #17  
Old 06-21-2023, 11:43 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

That works beautifully for creating the style sheet in the first instance, but it errors when trying to add additional selected text to the same style sheet. Is that an easy adjustment?

So, keep the existing functionality (which is spot on), but add that subsequently selected text is added to the end of the open stylesheet.

Thanks for your help! This will save me hours and hours each week!
Reply With Quote
  #18  
Old 06-22-2023, 01:26 AM
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: 4,166
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

I don't know what you selected after the first entry but it was working for me.

The code is copying unformatted text content to the StyleSheet docx and I thought it wouldn't matter if you shut it or kept it open.

If it is still throwing an error, can you work out what line it is failing on?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #19  
Old 09-15-2024, 08:28 AM
Sonicegorsan Sonicegorsan 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
Novice
 
Join Date: Sep 2024
Posts: 1
Sonicegorsan is on a distinguished road
Default

Quote:
Originally Posted by dfuent1321 View Post
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
When I tried to run code from the post I've got an error (Picture below). I found what it was caused by in my case. It was Punto Switcher program. It used for automatic keyboard layout switching, but also it has buffer history feature. I guess that some programs, that works with buffer remembering can somehow change it's content, so I would recommend to check all the programs that could interact with it and also checks programs, that could manipulate text formatting in buffer.
Reply With Quote
Reply

Thread Tools
Display Modes


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 PM.


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