Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-19-2014, 05:11 AM
Smallweed Smallweed is offline Selection.EndKey not working in 2010 Windows 7 64bit Selection.EndKey not working in 2010 Office 2010 32bit
Novice
Selection.EndKey not working in 2010
 
Join Date: Aug 2013
Posts: 13
Smallweed is on a distinguished road
Default Selection.EndKey not working in 2010

I've recently migrated some XP templates to 2010 and am now having problems with some of my VBA that should be inserting AutoText (which consists of some text and a table) at the end of the document. The code is:

Selection.EndKey Unit:=wdStory
ActiveDocument.AttachedTemplate.AutoTextEntries("m yStandardText") _
.Insert Where:=Selection.Range, RichText:=True

When the macro runs normally it's as if the Selection.EndKey line is being completely ignored because my AutoText ends up wherever the cursor happens to be.

The strange thing is that if I put a breakpoint anywhere before this line and then once it's stopped, press the run button/press F5, all runs as it should.

This almost feels like some kind of "refresh" issue, as if the code is just running too quickly and performing the insertion of the AutoText before the cursor gets the chance to move. I've tried putting an ActiveDocument.Repaginate before the above code but that doesn't help.

Any thoughts mightily appreciated...
Reply With Quote
  #2  
Old 08-19-2014, 06:41 AM
gmayor's Avatar
gmayor gmayor is offline Selection.EndKey not working in 2010 Windows 7 64bit Selection.EndKey not working in 2010 Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I can't see any good reason why it should not work from the clip you have posted, but you could try setting a range e.g.

Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("m yStandardText").Insert _
Where:=oRng, _
RichText:=True
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-19-2014, 07:08 AM
Smallweed Smallweed is offline Selection.EndKey not working in 2010 Windows 7 64bit Selection.EndKey not working in 2010 Office 2010 32bit
Novice
Selection.EndKey not working in 2010
 
Join Date: Aug 2013
Posts: 13
Smallweed is on a distinguished road
Default

Thanks Graham but it's still the same when I replace my code with yours.

As you say, there's no good reason from the clip I've posted. There's a lot of other VBA that gets processed before and my feeling is that the macro somehow gets ahead of itself. I'm going to try switching off multi-threading although I'm not sure if I'd be able to do that for the users.
Reply With Quote
  #4  
Old 08-19-2014, 07:11 AM
Smallweed Smallweed is offline Selection.EndKey not working in 2010 Windows 7 64bit Selection.EndKey not working in 2010 Office 2010 32bit
Novice
Selection.EndKey not working in 2010
 
Join Date: Aug 2013
Posts: 13
Smallweed is on a distinguished road
Default

Ah, I'm muddling with Excel 2010 which does let you turn off multithreading.

What's strange is that this code worked fine in XP.
Reply With Quote
  #5  
Old 08-19-2014, 07:21 AM
Smallweed Smallweed is offline Selection.EndKey not working in 2010 Windows 7 64bit Selection.EndKey not working in 2010 Office 2010 32bit
Novice
Selection.EndKey not working in 2010
 
Join Date: Aug 2013
Posts: 13
Smallweed is on a distinguished road
Default

I've managed to fix it - though no idea why this has worked! The code actually was as follows, typing a couple of hard returns and doing some other stuff before inserting the autotext:

With Selection
.EndKey Unit:=wdStory
.TypeText vbCr
.TypeText vbCr
End With
other code
ActiveDocument.AttachedTemplate.AutoTextEntries("m yStandardText") _
.Insert Where:=Selection.Range, RichText:=True

I tried commenting out the two .TypeText lines and it now works fine. I think we can survive without them: they were just there to ensure a gap before the autotext entry but there's usually a stray hard return or two at the bottom of the document anyway.

Makes no sense to me why this has fixed the problem but there we go. I better update all the other templates too.

By the way, there definitely isn't a space between the "m" and the "y" in my autotext name above as I type this but one keeps appearing when I post. Maybe it's because I'm using Firefox?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Selection.EndKey not working in 2010 Outlook 2010 rule not working khughes46 Outlook 1 05-06-2014 10:09 AM
Selection.EndKey not working in 2010 Selection of all Text for a specific page in word is spanning selection across pages ramsgarla Word VBA 9 12-05-2012 03:23 AM
Word 2010 - Specify working area Toni Chaffin Word 1 09-14-2012 02:46 AM
Outlook 2010 AutoConfigure not working vors Outlook 0 07-03-2012 08:46 AM
templates not working in 2010 normle Excel 0 01-24-2012 02:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:23 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft