![]() |
#1
|
|||
|
|||
![]() 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 |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Shelley Lou | Word VBA | 1 | 12-23-2021 06:24 AM |
![]() |
dogbural | Word VBA | 2 | 06-07-2020 04:12 PM |
Working with selection.tables | PRA007 | Word VBA | 3 | 02-25-2016 01:49 AM |
![]() |
PRA007 | Word VBA | 2 | 02-19-2016 12:52 AM |
![]() |
ramsgarla | Word VBA | 9 | 12-05-2012 03:23 AM |