Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-05-2022, 11:02 PM
m2ramos m2ramos is offline Selection.Information not working as intended for selection position Windows 10 Selection.Information not working as intended for selection position Office 2016
Novice
Selection.Information not working as intended for selection position
 
Join Date: Nov 2022
Posts: 2
m2ramos is on a distinguished road
Default Selection.Information not working as intended for selection position

Hello,



I'm fairly new to VBA coding and I have the code below that takes entries from a text file saved on local drive and searches a word document for matches. It then creates a hyperlink when it finds a string match. I've been playing around with a new subroutine that uses Selection.Information called getPosition. It should return the vertical and horizontal position of the current selection in reference to the page it's on. I called the sub in my code below within a Do While loop that searches for matches throughout the whole Active Document. I would expect the getPosition sub routine to display different numbers for each match it finds but right now, it's reporting the same position numbers for each match. I know on the page that these matches are in different positions. So I'm a bit confused. Can someone help me understand what I'm doing wrong?

Thanks.

Code:
Sub getPosition()
    Debug.Print Selection.Information(wdVerticalPositionRelativeToPage)
    Debug.Print Selection.Information(wdHorizontalPositionRelativeToPage)
End Sub

Sub CombinedTest()
    Dim directory As String
    Dim Raw As String
    Dim NumValues As Integer, J As Integer
    Dim strdocid As String
    Dim UserVals() As String
    'define the style
    Dim strStyle As String
    strStyle = "Subtle Emphasis"
    'set the search range
    Dim rngSearch As Range
    Set rngSearch = ActiveDocument.Range
    'set the search string - value set moved below
    Dim strSearch As String
    'set the target address for the hyperlink - value set moved below
    Dim strAddress As String
    
    directory = "C:\DaviesTmp\WIP\"
    Open (directory & "StringSearch.txt") For Input As #1
    Line Input #1, Raw
    NumValues = Val(Raw)
    ReDim UserVals(NumValues)
    
    For J = 1 To NumValues
        Set rngSearch = ActiveDocument.Range
        Line Input #1, UserVals(J)
        strdocid = Right(UserVals(J), 15)
    'strdocid finds DocID with extension counting in from right
        strSearch = Left(strdocid, 11)
    'strSearch finds DocID without extension using previous strdocid value counting in from left
        strAddress = UserVals(J)
        With rngSearch.Find
            Do While .Execute(findText:=strSearch, MatchWholeWord:=True, Forward:=True) = True
                With rngSearch 'we will work with what is found as it will be the selection
                getPosition
                ActiveDocument.Hyperlinks.Add Anchor:=rngSearch, Address:=strAddress
                    '.Style = ActiveDocument.Styles(strStyle) 'throw the style on it after the link-remove apostrophe at beginning if you need style change
                End With
                rngSearch.Collapse Direction:=wdCollapseEnd
                'keep it moving
            Loop
        End With
    Next J
    Close #1
End Sub
Reply With Quote
  #2  
Old 11-06-2022, 10:38 AM
gmaxey gmaxey is offline Selection.Information not working as intended for selection position Windows 10 Selection.Information not working as intended for selection position Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Finding text with a range object doesn't select the text so your selection is where it is throughout the process:

Code:
Dim oRng As Range
Sub getPosition()
    Debug.Print oRng.Information(wdVerticalPositionRelativeToPage)
    Debug.Print oRng.Information(wdHorizontalPositionRelativeToPage)
End Sub

Sub Test()
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "Test Words"
    While .Execute
      getPosition
      oRng.Collapse wdCollapseEnd
    Wend
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-06-2022, 10:57 AM
m2ramos m2ramos is offline Selection.Information not working as intended for selection position Windows 10 Selection.Information not working as intended for selection position Office 2016
Novice
Selection.Information not working as intended for selection position
 
Join Date: Nov 2022
Posts: 2
m2ramos is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Finding text with a range object doesn't select the text so your selection is where it is throughout the process:

Code:
Dim oRng As Range
Sub getPosition()
    Debug.Print oRng.Information(wdVerticalPositionRelativeToPage)
    Debug.Print oRng.Information(wdHorizontalPositionRelativeToPage)
End Sub

Sub Test()
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "Test Words"
    While .Execute
      getPosition
      oRng.Collapse wdCollapseEnd
    Wend
  End With
End Sub
Thanks for the update. With the information about select that you provided, I managed to get my code working by adding rngSearch.Select within the Do While Loop. That got me what I needed. Thank you.
Reply With Quote
  #4  
Old 11-07-2022, 05:54 AM
gmaxey gmaxey is offline Selection.Information not working as intended for selection position Windows 10 Selection.Information not working as intended for selection position Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Okay, but as my code example illustrates, you don't really have to select the found range.


oRng.Information returns the same thing as Selection.Information.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Selection.Information not working as intended for selection position VBA help With Selection.Range not working Shelley Lou Word VBA 1 12-23-2021 06:24 AM
Selection.Information not working as intended for selection position change header, footer information dependent on selection in drop down list dogbural Word VBA 2 06-07-2020 04:12 PM
Working with selection.tables PRA007 Word VBA 3 02-25-2016 01:49 AM
Selection.Information not working as intended for selection position Working with Selection.range. PRA007 Word VBA 2 02-19-2016 12:52 AM
Selection.Information not working as intended for selection position 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:36 PM.


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