Select a string based on page, line and column numbers

User avatar
macropod
4StarLounger
Posts: 524
Joined: 17 Dec 2010, 03:14

Re: Select a string based on page, line and column numbers

Post by macropod »

ErikJan wrote:
10 Jan 2025, 09:44
Stuart, Paul, I know what I'm looking for and I do realize that anything that changes e.g. the #chars will 'destroy' my approach. Also, it's not just pieces of text I need to 'restore' but more some pieces of text which had certain character-formatting applied.
You're really not giving anyone enough information to work with. I have shown you a simple way for storing text locations, which can then be retrieved for processing. The only response you gave was
ErikJan wrote:
08 Jan 2025, 11:10
Thanks, but in my case this doesn't work (for different reasons), I'm looking for a solution that works in the way I explained.
No mention of why it won't work, so all we're left with is guessing about what you're trying to do. I'm not into that game.

You also mention selecting the text for processing, but there is rarely any need to select anything in Word.

Perhaps you could attach a sample document to a post with a explanation of what you're trying to do so that we can better understand the problem and give informed advice on how you might address it.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
ErikJan
BronzeLounger
Posts: 1381
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Select a string based on page, line and column numbers

Post by ErikJan »

OK, I re-read my original question, which ended with ‘Note that any other method of storing info about certain pieces of text and then, based on that info, finding that text back is also perfectly fine’, and my simplified version of this question which I mentioned later in this thread: ‘with VBA, select the piece of text that is 15 characters long and can be found on page 2 in line 6, starting in column 8’.

Then I followed the thread as it developed. I see alternative suggestions but no direct answers to these -I still believe- straightforward questions. I was asked for background information which I tried to provide (high-level) but every time that seemed to only trigger suggestions to solve my larger problem (instead of providing an answer to my question). I tried to remain polite but, in the end, things seemed to get stuck and one of the responses I even perceived as offending and personal.

After some reflection I decided to re-engage, and provide the requested background by posting a document (attached) that shows the solution for my larger problem (‘Using one WORD document for different audiences’ – see also a thread in Eileen’s with the same name).
For clarity: as you can hopefully see, this solution works so I’m not looking for alternative solutions here.

To make this (working) approach easier to work with however, I had to solve some additional sub-problems. One was addressed in a thread in this forum called ‘Remove a character style’. A next (and last?) sub-problem was the question I posted in this thread.

Note that I wanted to post my full solution once all parts were solved, but regretfully that didn’t appear to be an option as I explained above.
The document (with code) posted in this response is step one and only provides a simple, working solution for my larger problem. Feel free to play with it (suggestions are always welcome but I’m not asking for anything at this moment in time).

In a few days / weeks, I’ll post a second document (with code), explaining the first sub-problem and demonstrating an -again: working!- solution. Also here, mainly documentation / explanation / background and no real question yet.
Then in hopefully a last step, I’ll describe the remaining problem (which will boil down to the question in this thread, closing the loop).
I hope that with the prior background, you will then understand what I’m looking for and why certain alternatives offered earlier in this thread won’t work here.

Disclaimer: the example code is re-written and simplified to demonstrate the intended behavior. I’m my larger, complex 80+ page document with many Tables, Figures, a Table of Figures and many cross references, I also have this fully working (with some additional coding).

Summarizing, I hope that by sharing my larger problem and examples of code I already have, you’ll have a better understanding of what I’m looking for and with that, you’ll be able to hopefully understand my question in this thread better and provide a solution.
You do not have the required permissions to view the files attached to this post.

User avatar
ErikJan
BronzeLounger
Posts: 1381
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Select a string based on page, line and column numbers

Post by ErikJan »

Here's the promised second post (including a Word document with some VBA code).

Here I present a way to 'promote' text to become part of the 'full' version of the document (which is done by applying the Character style mentioned before). As the style is applied, existing Character Styles in the text are erased and overwritten. The code will take care of this by first replacing them with local formatting.
For you information, also this part is working fine for me and I'm not looking for other solutions (although obviously enhancements and improvements are always welcome).
In a next (and hopefully) last post, I'll describe the problem for which I was looking for help when I started this thread (the opposite of the solution presented in this post: 'demoting' text to become part of the 'limited' version only).

I hope things are at least beginning to make more sense now.
You do not have the required permissions to view the files attached to this post.

User avatar
SpeakEasy
5StarLounger
Posts: 742
Joined: 27 Jun 2021, 10:46

Re: Select a string based on page, line and column numbers

Post by SpeakEasy »

>select the piece of text that is 15 characters long and can be found on page 2 in line 6, starting in column 8

Without querying exactly why you want/need to do this, here is a piece of code that can do exactly what this says. How actually useful it is, I can't say.

Code: Select all

Public Function SelectCharacterByPageLineChar(pageNum As Integer, lineNum As Integer, charPos As Integer, Optional charcount As Integer = 1, Optional highlight As Boolean) As String
    Dim doc As Document
    Dim rng As Range
    Dim lineStart As Range
    Dim charIndex As Long

    'Set doc = ActiveDocument
    Set rng = ActiveDocument.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageNum) ' Move to page

    With rng
        ' Loop through document to find the specific line on the page. Move methods with wdLine prone to failure
        Do
            If .Information(wdFirstCharacterLineNumber) = lineNum Then Exit Do
            .MoveEnd Unit:=wdCharacter, Count:=1 ' Move forward character by character
            .Collapse wdCollapseEnd
        Loop While .Information(wdActiveEndPageNumber) = pageNum ' Stop if we leave the page
    
        If rng.Information(wdFirstCharacterLineNumber) <> lineNum Then
            MsgBox "Line not found on page " & pageNum, vbExclamation
            Exit Function
        End If
    
        ' Now that we are at the correct line, move to the specific character
        .MoveStart Unit:=wdCharacter, Count:=charPos - 1 ' Move to the character
        .MoveEnd Unit:=wdCharacter, Count:=charPos + charcount - 2 ' Extend range to charcount '
        
        ' Select the character(s) if required
        If highlight Then .Select
        SelectCharacterByPageLineChar = .Text
    End With
End Function