Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA to move to end of pasted selection

Posted on 2014-01-18
6
Medium Priority
?
550 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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 15

Accepted Solution

by:
DrTribos earned 1200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 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