VBA Copy Text with Selection - not Bookmark

J

Jenny

Alter below macro from using a bookmark as the SourceRange to a Selection
(still ActiveDocument) as the Source Range without much ado - how to please?

Sub Copy_Text_Demo_5()
'Copy a range from one place to another

Dim NewDoc As Document, OldDoc As Document
Dim SrcRg As Range, DestRg As Range

Set OldDoc = ActiveDocument
Set NewDoc = Documents.Add

Set SrcRg = OldDoc.Bookmarks("bk").Range
Set DestRg = NewDoc.Range

DestRg.FormattedText = SrcRg.FormattedText

End Sub
 
J

Jenny

Just to add, this macro is from Jay Freedman. I have an error handling save
selection macro with format - I just think this is works so well I want to
see if I can use it for snippets of text as I work.

Many thanks.
 
L

Lene Fredborg

I am not totally sure I understand your description correctly. Do you want to
copy the currently selected content from the active document to a new
document?

In that case, I think the easiest solution is still to use a bookmark – but
in this case you must use one of the predefined bookmarks (for more details
about the predefined bookmarks, see the VBA Help). One of those bookmarks is
“\Selâ€, that contains the current selection, or – if nothing is selected –
the insertion point.


The macro below will do the job:

Sub Copy_Text_Demo_5_SELECTION()
'Copy a range from one place to another

Dim NewDoc As Document, OldDoc As Document
Dim SrcRg As Range, DestRg As Range

Set OldDoc = ActiveDocument
Set SrcRg = OldDoc.Bookmarks("\Sel").Range

Set NewDoc = Documents.Add
Set DestRg = NewDoc.Range

DestRg.FormattedText = SrcRg.FormattedText

End Sub


Very little has been changed compared to the version you posted:
1. The line
Set SrcRg = OldDoc.Bookmarks("bk").Range
has been replaced by the line
Set SrcRg = OldDoc.Bookmarks("\Sel").Range

2. The above line has been moved before the line that adds the new document.
Otherwise you would have to include code like “OldDoc.Activate†first in
order to activate the source document first since the Selection works on the
currently active document (after adding the new document, it becomes the
active document).

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
G

Graham Mayor

If you want to copy a snippet of text, then you need to redefine the range
srcRg to the selected snippet eg

Sub Copy_Text_Demo_5()
'Copy a range from one place to another
Dim NewDoc As Document, OldDoc As Document
Dim SrcRg As Range, DestRg As Range
Set OldDoc = ActiveDocument
Set SrcRg = Selection.Range
Set NewDoc = Documents.Add
Set DestRg = NewDoc.Range
DestRg.FormattedText = SrcRg.FormattedText
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jenny

Graham works super fast thank you so much.

Graham Mayor said:
If you want to copy a snippet of text, then you need to redefine the range
srcRg to the selected snippet eg

Sub Copy_Text_Demo_5()
'Copy a range from one place to another
Dim NewDoc As Document, OldDoc As Document
Dim SrcRg As Range, DestRg As Range
Set OldDoc = ActiveDocument
Set SrcRg = Selection.Range
Set NewDoc = Documents.Add
Set DestRg = NewDoc.Range
DestRg.FormattedText = SrcRg.FormattedText
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top