#1
|
|||
|
|||
Help using VBA to find a hidden text string
I can't figure out how to use VBA to find a hidden text string.
The following code is a macro I coded (unsuccessfully) trying to use tips in several postings I found on the web. I included a couple of comment lines I put into the macro so that I'd be able to refer to the examples I was trying (unsuccessfully) to emulate. Code:
Sub subBkMarkFindAndBkMarkNextTblOfExhibitsPara() 'https://benread.net/tools/macros-word-general.html 'https://www.office-forums.com/threads/finding-hidden-text-in-word-vba.2349850/ Application.ScreenUpdating = False Dim bShow As Boolean bShow = ActiveWindow.View.ShowHiddenText ActiveWindow.View.ShowHiddenText = True With Selection.Find .ClearFormatting .Text = "[FULLNAME: PDF] " .MatchWildcards = False .Forward = False .Style = ActiveDocument.Styles("Heading 1") .Execute .ClearFormatting .Forward = True End With With Selection.Find .ClearFormatting .MatchWildcards = False .Text = "[FULLNAME: PDF] " .Forward = True .Wrap = wdFindStop .Format = False .Font.Hidden = True If .Execute Then 'I guess "If .Execute = True", your code continues processing from here. Selection.HomeKey Unit:=wdLine, Extend:=wdMove With ActiveDocument.Bookmarks .Add Range:=Selection.Range, Name:="LastEntryThusFarMadeInTblOfExhibits" .DefaultSorting = wdSortByName .ShowHidden = False End With 'With ActiveDocument.Bookmarks Else ' If .Execute Then MsgBox "Message from Macro: NMc:subBkMarkFindAndBkMarkNextTblOfExhibitsPara()" & _ "[FULLNAME: PDF] wasn't found." End If 'If .Execute Then End With 'With Selection.Find ActiveWindow.View.ShowHiddenText = bShow Application.ScreenUpdating = True Call subBkMarkNextEntryInTblOfExhibits End Sub 'subBkMarkFindAndBkMarkNextTblOfExhibitsPara |
#2
|
||||
|
||||
You haven't actually said what you're trying to achieve.
From the code, though, it appears you're trying to bookmark the first instance of '[FULLNAME: PDF] ' that occurs after the last Heading 1 text preceding the current selection that contains the same string, regardless of what Style that latter string has applied to it. Please clarify.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
1. Cannot search for "hidden" text; 2. How to search for the next "Heading 1"
You are right. I am trying to bookmark the first instance of '[FULLNAME: PDF] ' that occurs after the last Heading 1 text preceding the current selection.
I encounter 2 problems: Problem #1: My search for the text string '[FULLNAME: PDF] ' fails because that text is hidden text. I don't know if "hidden" is called an text "attribute", or what is the right name for the collection that contains the rubric "hidden". Problem #2: I also do not know how to search for the next "Heading 1" following my current selection point. Thank you for taking the time to help me. Much appreciated. Marc |
#4
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim bShow As Boolean, RngFnd As Range bShow = ActiveWindow.View.ShowHiddenText ActiveWindow.View.ShowHiddenText = True With ActiveDocument.Range If .Bookmarks.Exists("LastEntryThusFarMadeInTblOfExhibits") Then .Bookmarks("LastEntryThusFarMadeInTblOfExhibits").Delete End If With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[FULLNAME: PDF] " .Replacement.Text = "" .Forward = False .Wrap = wdFindStop .Execute End With Do While .Find.Found If .Style = "Heading 1" Then If Selection.Paragraphs.First.Range.Start >= .Start Then Exit Do End If Set RngFnd = .Duplicate .Collapse wdCollapseStart .Find.Execute Loop If Not RngFnd Is Nothing Then .Bookmarks.Add Name:="LastEntryThusFarMadeInTblOfExhibits", Range:=RngFnd End With ActiveWindow.View.ShowHiddenText = bShow Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
The Demo() macro did not create a new bookmark "LastEntryThusFarMadeInTblOfExhibits"
The Demo() macro did remove the preexisting bookmark "LastEntryThusFarMadeInTblOfExhibits", but did the macro did not create a new bookmark "LastEntryThusFarMadeInTblOfExhibits".
I bet your next response will accomplish that. But alas, no good deed goes unpunished. Your Demo() macro contained a lot of code that I do not understand ... because I do not understand how ranges work, and I have never found a tutorial explaining ranges (and how to use them) that I could understand enough to be able to use ranges. I think your code between the line: Do While .Find.Foundand the line: If Not RngFnd Is Nothing Then .Bookmarks.Add Name:="LastEntryThusFarMadeInTblOfExhibits", Range:=RngFndwas intended to do the following: 1. Create something analogous to a "selection point" at the place in the document where the search found the text "[FULLNAME: PDF] ", and 2. Create the new bookmark "LastEntryThusFarMadeInTblOfExhibits" at that new "selection point" ... or something like that. I would like the macro to insert the new bookmark "LastEntryThusFarMadeInTblOfExhibits" in column #1 on the first line of the next Heading 1 where the search found the text "[FULLNAME: PDF] " (i.e., the Heading 1 paragraph, where the search found the text "[FULLNAME: PDF] ", after the Heading 1 paragraph where the preexisting bookmark "LastEntryThusFarMadeInTblOfExhibits" had been, before it was deleted by the macro). But even more than that, I would like to understand how to do it. So, I would appreciate any suggestions (e.g., online tutorials or Reference URLs, or Google search words) you could give me to help me find materials so that I could understand how you are using the "range" thing to get where we want to be. Lastly, can you only have one range in existence at a time? One more thing, why is the sky blue? Thanks again, Marc |
#6
|
||||
|
||||
Quote:
Quote:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim Rng As Range With ActiveDocument Set Rng = Selection.Characters.Last Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="LastEntryThusFarMadeInTblOfExhibits") Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") Rng.Collapse wdCollapseEnd Rng.End = .Range.End With Rng With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[!^13]@[FULLNAME: PDF] " .Style = "Heading 1" .Font.Hidden = True .Replacement.Text = "" .Forward = True .Format = False .Wrap = wdFindStop .MatchWildcards = True .Execute End With If .Find.Found = True Then .Collapse wdCollapseStart .Bookmarks.Add Name:="LastEntryThusFarMadeInTblOfExhibits", Range:=.Duplicate End If End With End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
My revns to your DATE Demo get the rest of what I need, crudely
I made some revisions to your Demo posted on 03-06-2020, 08:32 PM
2020-03-06_20-32: Quote:
The following is your Demo() crudely revised by me: Quote:
I always get the error message (MsgBox "something went wrong here."), but the new bookmark does get inserted where I want it to be inserted. Obviously, my code does not search for the next paragraph that is formatted with Heading 1 AND that contains the Text = "[!^13]@[FULLNAME: PDF] " ... so it would be great if you could show me how to do that ... since you obviously have nothing else to do with your time besides help me. (Joke) Lastly, I want to: Apologize for being so poor at expressing what I was trying to do; and Thank you again, very sincerely. Marc |
#9
|
||||
|
||||
So what is wrong with the code in post #7?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
hidden text, vba code, vba find and replace |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to find all string within string. | PRA007 | Word VBA | 18 | 02-12-2016 08:11 PM |
VBA to Find and Format Text string defined using Inputbox within selection | sistemalan | Word VBA | 7 | 10-03-2014 07:28 AM |
Way to search for a string in text file, pull out everything until another string? | omahadivision | Excel Programming | 12 | 11-23-2013 12:10 PM |
Find and replace a string of text | errtu | Word | 1 | 01-31-2013 02:09 PM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |