Solved

VBA to move to end of pasted selection

Posted on 2014-01-18
6
461 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
  • 4
  • 2
6 Comments
 
LVL 14

Expert Comment

by:DrTribos
ID: 39791827
-
0
 
LVL 14

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 14

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 14

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Outlook Free & Paid Tools
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now