Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-15-2014, 11:15 PM
NobodysPerfect NobodysPerfect is offline Selection (range) in Word or Excel table Windows 8 Selection (range) in Word or Excel table Office 2010 32bit
Competent Performer
Selection (range) in Word or Excel table
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default Selection (range) in Word or Excel table

Hi to all,

I'm looking for a short and reliable way to determine if the current selection is within a Word table or a "{ LINK Excel.Sheet ... }" Excel table.

Selection.Information in both cases is wdWithinTable.
.Parent in both cases is the current document.
.Creator and .Application in both cases is Word.

So where is the difference?

Thanks for any help


NP
Reply With Quote
  #2  
Old 09-15-2014, 11:48 PM
macropod's Avatar
macropod macropod is offline Selection (range) in Word or Excel table Windows 7 64bit Selection (range) in Word or Excel table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What you really need to do here is to determine whether the selection is within a field. The following function does that.
Code:
Function WithInField(Rng As Word.Range) As Boolean
' Based on code by Don Wells: http://www.eileenslounge.com/viewtopic.php?f=30&t=6622
' Approach  : This procedure is based on the observation that, irrespective of _
              a field's ShowCodes state, toggling the field's ShowCodes state _
              twice collapses the selection to the start of the field.
Dim lngPosStart As Long, lngPosEnd As Long, StrNot As String
WithInField = True
Rng.Select
lngPosStart = Selection.Start
lngPosEnd = Selection.End
With Selection
  .Fields.ToggleShowCodes
  .Fields.ToggleShowCodes
  ' Test whether the selection has moved; if not, it may already have been _
    at the start of a field, in which case, move right and test again.
  If .Start = lngPosStart Then
    .MoveRight
    .Fields.ToggleShowCodes
    .Fields.ToggleShowCodes
    If .Start = lngPosStart + 1 Then
      WithInField = False
    End If
  End If
End With
End Function
You can use the function with code like:
Code:
Sub TestWithInField()
Dim Rng As Word.Range, c As Word.Range, StrRslt As String
Set Rng = Selection.Range
For Each c In Rng.Characters
    StrRslt = StrRslt & c.Text & ",WithInField:" & WithInField(Rng:=c) & vbCr
Next
Rng.Select
MsgBox StrRslt
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-16-2014, 12:06 AM
NobodysPerfect NobodysPerfect is offline Selection (range) in Word or Excel table Windows 8 Selection (range) in Word or Excel table Office 2010 32bit
Competent Performer
Selection (range) in Word or Excel table
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Hi Paul,

thanks for your answer. It is excactly as I feared: I need 'field testing'. So no real "short way" ... I will use your (almost) ready to use code .

Have a nice day
NP




Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Selection or Range Tommes93 Word VBA 1 04-10-2014 02:50 AM
Selection (range) in Word or Excel table 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
Selection (range) in Word or Excel table Word VBA: Cannot Edit Range (Delete characters except the first in a table cell) tinfanide Word VBA 3 04-27-2012 09:48 AM
Selection (range) in Word or Excel table Set range for merged Word table cells? tinfanide Word VBA 1 02-06-2012 05:57 AM
Selection (range) in Word or Excel table you cannot insert this selection into a table. cs_starter Mail Merge 2 09-08-2011 02:58 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:51 AM.


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