Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-04-2020, 11:01 AM
marceepoo marceepoo is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 64bit
Novice
Help using VBA to find a hidden text string
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Default 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

Reply With Quote
  #2  
Old 03-04-2020, 02:25 PM
macropod's Avatar
macropod macropod is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 03-05-2020, 08:30 PM
marceepoo marceepoo is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 64bit
Novice
Help using VBA to find a hidden text string
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Default 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
Reply With Quote
  #4  
Old 03-05-2020, 09:28 PM
macropod's Avatar
macropod macropod is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 03-06-2020, 02:54 PM
marceepoo marceepoo is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 64bit
Novice
Help using VBA to find a hidden text string
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Smile 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.Found
and the line:
If Not RngFnd Is Nothing Then .Bookmarks.Add Name:="LastEntryThusFarMadeInTblOfExhibits", Range:=RngFnd
was 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
Reply With Quote
  #6  
Old 03-06-2020, 03:09 PM
macropod's Avatar
macropod macropod is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by marceepoo View Post
The Demo() macro did remove the preexisting bookmark "LastEntryThusFarMadeInTblOfExhibits", but did the macro did not create a new bookmark "LastEntryThusFarMadeInTblOfExhibits".
It does both in my testing.
Quote:
Originally Posted by marceepoo View Post
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.Found
and the line:
If Not RngFnd Is Nothing Then .Bookmarks.Add Name:="LastEntryThusFarMadeInTblOfExhibits", Range:=RngFnd
was 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.
What happens when using Find is that the range spanned by Rng actually moves to the found range.
Quote:
Originally Posted by marceepoo View Post
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).
Well, that is rather different from what you previously described. I'll get back to you on that.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 03-06-2020, 08:32 PM
macropod's Avatar
macropod macropod is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #8  
Old 03-08-2020, 02:43 PM
marceepoo marceepoo is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 64bit
Novice
Help using VBA to find a hidden text string
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Default 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:
Originally Posted by macropod View Post
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

The following is your Demo() crudely revised by me:
Quote:
Sub subMacropodDemo202003062032_MarcRevn01()
'2020-03-06_20-32 202003062032 subMacropodDemo202003062032()
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
.Select
.Collapse wdCollapseStart
'.Bookmarks.Add Name:="LastEntryThusFarMadeInTblOfExhibits", Range:=.Duplicate
Else
MsgBox "something went wrong here."
End If
End With
Selection.HomeKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
' .Bookmarks.Add Name:="LastEntryThusFarMadeInTblOfExhibits", Range:=.Duplicate


With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="LastEntryThusFarMadeInTblOfExhibits"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With


End With
Application.ScreenUpdating = True
End Sub 'subMacropodDemo202003062032_MarcRevn01()

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
Reply With Quote
  #9  
Old 03-08-2020, 03:24 PM
macropod's Avatar
macropod macropod is offline Help using VBA to find a hidden text string Windows 7 64bit Help using VBA to find a hidden text string Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

So what is wrong with the code in post #7?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

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
Help using VBA to find a hidden text string 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
Help using VBA to find a hidden text string Find and replace a string of text errtu Word 1 01-31-2013 02:09 PM
Help using VBA to find a hidden text string Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft