Solved

VBA to move to end of pasted selection

Posted on 2014-01-18
6
499 Views
Last Modified: 2014-01-27
I've got a Word (2010) macro that helps the user copy and paste content from a source document to a destination document.  With the 2 docs in side-by-side mode, they first select text and click a button which copies the selection and gives them a dialog telling them to put their insertion point in the destination document and click the paste button.  

After checking to make sure they are in the destination document, the macro pastes the selected text into the destination doc, at the same time formatting it to match the normal style of the destination doc.  Then it goes back to the original selection in the sourcedoc and changes the copied text to strikeout so the user knows it has been copied.

Works like a charm with one exception:  In the destination doc, it leaves my insertion point at the START of the pasted text, but I want it to be at the END of the pasted text.  I've tried a few things and can't get it to do that.  I borrowed parts of this code so I'll admit I don't know exactly how the paste and format part works, which is why I'm struggling with this I'm sure.  

Private Sub PasteButton_Click()

If Selection.Document = copydoc Then
    MsgBox "please select a location in the Desination document."
    Exit Sub
End If

Dim oRng As Range, oStart As Range
Set oRng = Selection.Range
Set oStart = Selection.Range
With oRng
    .PasteSpecial DataType:=wdPasteRTF, Placement:=wdInLine
    .Start = oStart.Start
    .Font.Name = ActiveDocument.Styles("Normal").Font.Name
    .Font.Size = ActiveDocument.Styles("Normal").Font.Size
    .Font.ColorIndex = ActiveDocument.Styles("Normal").Font.ColorIndex
End With

copydoc.Activate
Selection.Font.StrikeThrough = wdToggle
Selection.MoveRight unit:=wdCharacter, count:=1

Unload Me

End Sub

Open in new window


I've tried oRng.MoveEnd or Selection.MoveEnd, but they seem to be referencing the original selection in the sourcedoc.  

Can anybody help me figure out how to end up at the end of the pasted selection?

Thanks.
0
Comment
Question by:versatilebb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 15

Expert Comment

by:DrTribos
ID: 39791827
-
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39791839
Sub pastebefore()

Dim rng1 As Range
Dim rng2 As Range

Set rng1 = Selection.Range
Set rng2 = rng1.Duplicate
rng2.End = rng2.End + 1
rng2.Collapse wdCollapseEnd
rng1.Paste

rng2.Select



End Sub
0
 

Author Comment

by:versatilebb
ID: 39795933
Thanks for your post.  It does indeed put me at the end of the pasted text, but I'm lost as to how it would fits with the rest of my code above.  I still need the part that formats the pasted text and strikes through the copied text, but it's not clear where I would put that in the code you're suggesting.  

Can you please give me some guidance on where and how to use your suggestion?  Does it take the place of part of my code, and if so, which part??

Thanks
0
Windows running painfully slow? Try these tips..

Stay away from Speed Up Computer Programs that do more harm than good.
Try these tips instead.
Step by step instructions in trouble shooting Windows Performance issues.

 
LVL 15

Accepted Solution

by:
DrTribos earned 400 total points
ID: 39796162
Hi I have not had a chance to test this - basically your code goes in between.  The extra code just makes a 2nd range immediately after the first range giving you something to select.  Since we are working with range objects you should be able to change the selection at any time without impacting on the range.

Hope it helps
Private Sub PasteButton_Click()

If Selection.Document = copydoc Then
    MsgBox "please select a location in the Desination document."
    Exit Sub
End If
'---
Dim rng1 As Range
Dim rng2 As Range

Set rng1 = Selection.Range
Set rng2 = rng1.Duplicate
rng2.End = rng2.End + 1
rng2.Collapse wdCollapseEnd
'rng1.Paste
' should be ok to select rng2 here as you are working with the range object
' from here on (not the range) but decided to select rng2 later 
' rng2.Select


'---
'Dim oRng As Range, oStart As Range
'Set oRng = Selection.Range
'Set oStart = Selection.Range
With rng1 'oRng
    .PasteSpecial DataType:=wdPasteRTF, Placement:=wdInLine
    .Start = rng1.start 'oStart.Start
    .Font.Name = ActiveDocument.Styles("Normal").Font.Name
    .Font.Size = ActiveDocument.Styles("Normal").Font.Size
    .Font.ColorIndex = ActiveDocument.Styles("Normal").Font.ColorIndex
End With

' while you are still in the destination document you need to select the new range
rng2.select

copydoc.Activate
Selection.Font.StrikeThrough = wdToggle
Selection.MoveRight unit:=wdCharacter, count:=1  'this is working in your copy document

Unload Me

End Sub

Open in new window

0
 

Author Comment

by:versatilebb
ID: 39808112
Thanks, your code did leave me at the end of the pasted selection, but none of the formatting commands after I paste (font name, size, color) have any effect.  

I'm using a workaround where I insert some invisible characters before I paste in the text, which get pushed to the end of the pasted text, then search for them and delete them.  A bit primitive but it works.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39808364
It works on my system when, for example I change the font size to 16 (instead of pulling the attribute from the Normal font).  Could it be that your normal font is different to what you expect?

Hope this helps
0

Featured Post

SendBlaster Pro 4 - Bulk Email Sending Software

SendBlaster 4 Pro - Best Bulk Emailing Sending Software
Automatic Subscribe / Unsubscribe Processing
Great for Newsletters & Mass Mailings
Optional HTML & Text Composition
Integration with Google Features
Built in Spam Score Checking
Free Professional Templates - Feature Packed!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question