Solved

VBA to move to end of pasted selection

Posted on 2014-01-18
6
515 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:Bryce Bassett
[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:Bryce Bassett
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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:Bryce Bassett
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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