Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA to move to end of pasted selection

Posted on 2014-01-18
6
Medium Priority
?
584 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

783 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