Select a string based on page, line and column numbers

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

Select a string based on page, line and column numbers

Post by ErikJan »

I have some code that changes formatting in a WORD document. For certain pieces of text I want to 'fix' some small adverse results of that formatting after the procedure is done. To do that, first my code finds all these parts and stores page#, line#, column # and text length in an array.
Now I 'change' the formatting. All of that works OK.
Last part is to find that stored text-parts back, based on the location information in my array and then make a small fix (correction).
How do I select text in Word based on page, line, column and length?
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.

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

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

Post by snb »

Delve into docvariables.

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 »

If only the formatting is the issue, you don't need to know anything about the page#, line#, or column# - all you need to know is the range the object spans. For example, if you want to find wherever a particular string occurs in the document:

Code: Select all

Sub Demo()
Application.ScreenUpdating = False
Dim StrRng As String
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = InputBox("What is the Text to Find")
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Do While .Find.Execute
    StrRng = StrRng & .Start & ":" & .End & " "
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
MsgBox StrRng & "ranges found."
End Sub
Paul Edstein
[Fmr MS MVP - Word]

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

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

Post by snb »

See PM.
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 »

macropod wrote:
06 Jan 2025, 21:47
If only the formatting is the issue, you don't need to know anything about the page#, line#, or column# - all you need to know is the range the object spans. For example, if you want to find wherever a particular string occurs in the document:

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.

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 »

snb wrote:
07 Jan 2025, 11:47
See PM.
Interesting and (very) simple indeed. Thank you. Let me play with this a bit. I'll let you know if this works for my case.

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 »

So how do I add pieces of text to the 'hidden' section and how do I remove it again (all without impacting formatting)?

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

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

Post by snb »

Toggle between 'field content visible' and 'field content hidden': Alt-F9
When the field content is visible you can adapt the text ad libitum.

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 »

Yes, that's an answer to my question indeed. But that's not what I meant.
What I mean is "How do I turn non-hidden text into hidden text" and the other way round.

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

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

Post by snb »

By changing the value of docvariable 'naam'. See the macro in the file.
And by changing the condition in the 'IF'-Field.

And by using 'update fields': F9 (in VBA: thisdocument.fields.update)

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 »

That's not what I meant. I'm sorry. I completely understand your example and code. Let me try again, this time more verbose.

I have a piece of text that is labeled as 'hidden' (so if the docvariable is not set, it's not visible). Every once in a while, I'd like to 'promote' parts, so I might want this piece of text to be no longer hidden but always visible. To do that manually, I would have to select the text inside the docvariable field, copy it and then paste it back in the document 'outside' the field. Then I can delete the field. That procedure 'promotes' the piece of text to become 'always visible'.
The reverse is also possible: I started to write a piece of text that later I want that to become part of the hidden-collection. There I should probably create a docvariable field first and then cut and paste the text in there.
Manually is certainly an option, but if this could be done with VBA on a piece of selected text...

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

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

Post by snb »

You started your thread asking for a solution to present to several 'readers' a mix of general and individual texts. The method I showed you is based on a proper design of the main document.
Your last question seems to address quite another theme: how to use VBA in the process of designing that main document. I can't imagine a situation in which you have to adapt the design of the document so often or so drastically that it's worth while to use VBA for that.

Instead you could store all text elements in separate docvariables.
You can easily copy, move, delete docvariables in the document.
The only restriction is that you can only fill those docvariables using VBA (e.g Thisdocument.variables("Text1")="dit is de eerste tekst")

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

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

Post by snb »

The macro M_snb shows how to store text elements in docvariables.
You only need to do this once, the contents of all docvariables will be stored in the document.
The document shows how you can manipulate docvariables.
There's 1 caveat: you can't store formatting in a docvariable. You can format the complete text of a docvariable though.
You do not have the required permissions to view the files attached to this post.
Last edited by snb on 09 Jan 2025, 15:40, edited 1 time in total.

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 »

The document I'm talking about is a 'living' document where I'm regularly adding, updating and removing parts. That also involves 'promoting' and 'demoting' sections.
You provided a way to hide and show certain parts (and that works) and all I was adding if there would be a way to 'promote' or 'demote' part (and true: I added that).
My original thread question however was NOT about this at all (this was a question I asked in another thread, and I already have a solution that -almost- works).
My thread question here was how I could 'undo' generic formatting to pieces of text in a document after some document-wide changes. That has nothing to do with hidden or non-hidden sections (as said: that is another thread. Your feedback and suggestion made sense, so I tried to test that). My original question for this thread remains unanswered however.

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

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

Post by snb »

Without a sample file I have no clue.

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 »

The title of this thread "Select a string based on page, line and column numbers" sort of says it all. Not sure what I can add to that.
I could create a file with text and then ask: "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". But you don't need a file for that I guess...

User avatar
StuartR
Administrator
Posts: 12949
Joined: 16 Jan 2010, 15:49
Location: London, Europe

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

Post by StuartR »

There's a risk that format changes in one part of the document will cause strings later in the document to move. You could minimize the risk of this by working backwards from the end of the document, but things like page, line, and column position are not a stable way to locate a string.
StuartR


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 »

StuartR wrote:
09 Jan 2025, 22:16
things like page, line, and column position are not a stable way to locate a string.
Which is why I suggested working with the ranges they occupy. Aside from which, working with ranges identified the way I suggested is far more straightforward than using Goto to go to the page, then Goto to go to the line, then moving the range start & end to go to the column(s).
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 »

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.
I indicated that I think I have a good approach (read: I know where all these locations are, and I have stored that in an array) and all I was asking is how I can use the information in that array to re-select these text-elements.
Weirdly enough, answers I get range from hiding text to warnings that this might or might not work. Of course that is appreciated as always, but it doesn't address my question which, I think, -again- might be simple as I worded it a few posts back: "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".
If I get hints or suggestions that would help me solve just that, I can try that out in my specific case. And admitted: maybe I don't see the complexity and realize only then that there might be additional problems. But maybe it can also work for me and then the question is answered, and my problem is solved.
If there's no way to select text in WORD VBA based on this info, I can accept that and then I need to start thinking about other approaches.

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

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

Post by snb »

I have some code that changes formatting in a WORD document.
which code , which formatting
For certain pieces of text I want to 'fix' some small adverse results of that formatting after the procedure is done.
which pieces of text, what kind of 'fix', what is an 'adverse result'
To do that, first my code finds all these parts and stores page#, line#, column # and text length in an array.
How does it find these parts, how can these parts be detected. what code do you use to store page, line, column and textlength.
Now I 'change' the formatting. All of that works OK.
What does 'change' mean ? What kind of formatting. Why the need to fix a result that is OK ?
Last part is to find that stored text-parts back, based on the location information in my array and then make a small fix (correction).
Why does your 'change' create a result that has to be corrected ?? What is this 'small fix' ?

Your questions lack clarity, lack illustration by a sample document, lack information on the goal you are trying to achieve and lack the context/purpose of what you are doing.
This is called a 'salami-approach'.
If it's an MIVD-project you'd better tell us.
At least your questions suggest that you are overcomplicating simple things by your lack of knowledge of Word's/VBA's methods, procedures, functions.
Your questions indicate above all a lack of sound structure of the project you are working on.