|
|
Thread Tools | Display Modes |
#16
|
||||
|
||||
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 |
#17
|
|||
|
|||
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! |
#18
|
||||
|
||||
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 |
#19
|
|||
|
|||
Thanks for this solution. Someone else offered a solution earlier that worked, so I'll just keep using it. But I truly appreciate the help!
best, David |
#20
|
||||
|
||||
Looking back at the code I supplied, I expect it would error if the StyleSheet was already created but closed again and then the macro was run. I should have added a test to make sure the file didn't already exist before trying to save a new file over the top of it (by opening the existing style sheet instead of creating a new doc).
Since you already have a working replacement I won't bother fixing that problem in the code above.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#21
|
|||
|
|||
Nevermind this post. Can't find out how to delete, so I've edited.
|
|
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 |