Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-27-2024, 06:54 AM
RRB's Avatar
RRB RRB is offline replacing text in a variable Windows 11 replacing text in a variable Office 2021
Susan Flamingo
replacing text in a variable
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default replacing text in a variable

I was really trying to do this myself, but alas I got stuck.



I want to insert a bookmark at the present location with the name of the selected text.

so we have selText=selection.text

But of course, we cannot have any spaces in a BM name. I also need the current page number as part of the BM name

so we have selText=selection.text
So we need to turn "my selected text" into "01_my_selected_text"

The rest I think I can handle.

Thank you and have a good day!

Susan Flamingo

Update Edit:

Update Edit:
I was able to do this (hurray for me!!

Dim SelectedText As String
Dim BMN As String
Dim pgNo As Integer
Dim newPN As String
SelectedText = Selection.Text
pgNo = Selection.Range.Information(wdActiveEndPageNumber)
newPN = CStr(pgNo)
BMN = Replace(SelectedText, " ", "_")
'BMN = newPN & "_" & BMN
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="pg" & newPN & "_" & BMN
End With

Is this high quality code?
Reply With Quote
  #2  
Old 02-27-2024, 05:20 PM
Charles Kenyon Charles Kenyon is offline replacing text in a variable Windows 11 replacing text in a variable Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Hi Susan,
I am no one to judge the quality of code (but Andrew is, IMO).


Keep in mind that the concept of "page" is a very slippery / nebulous concept when working in Word. I have no clue why you want it part of a bookmark name.

Word Doesn't Know What a Page Is by Daiya Mitchell, MVP
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pasting in text, selecting that text, replacing words within that text but adding numbers too ctviggen Word VBA 0 03-25-2023 11:56 AM
I am automating word using vba. Need Help replacing paragraph with variable information Puneet Singh Word VBA 18 02-08-2022 10:48 PM
replacing text in a variable ord vba replace the variable text with variable images to make offer products with images tanzinim Word VBA 4 12-30-2015 01:40 PM
Replacing the 2nd or 3rd instance of Text wdillon2 Word VBA 2 05-13-2015 10:21 PM
Replacing / editting text LisaC Word 0 02-25-2010 03:40 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:48 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