Change multiple characters in a textframe at a time in vbs

Participant ,
Feb 21, 2022 Feb 21, 2022

Copy link to clipboard

Copied

 

I can use "characters" properties as follows to change a character contents in a textframe:

doc.TextFrames(1).Characters(6).Contents = "F"

But I don't know how to change multiple characters in a textframe at a time (For example, "12345678" change to "12abcd78" )?

At the same time, I found that the following codes have the same function. I don't know the difference between them (Characters and TextRanges properties)?

doc.TextFrames(1).TextRanges(6).Contents = "F"

Can anyone know this and help me?

TOPICS
Scripting

Views

83

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 21, 2022 Feb 21, 2022

Copy link to clipboard

Copied

A TextRange can be a character, or the whole text content of the text box.

The TextRanges collection always appears to show individual characters. Words and Lines can have characters, I am not 100% sure that in all cases all characters would have an exact match via the TextRanges items.
To change the contents it is very easy, but to keep formatting is a whole another story.

 

Here is an example of being able to replace the contents.

Set MyText = Doc.TextFrames(1)
MyText.Contents = Replace(MyText.Contents, "abc", "HEllo, World!")

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 11, 2022 Mar 11, 2022

Copy link to clipboard

Copied

LATEST

Thanks, Silly-V. 

For some reason, I couldn't reply to the forum post for a long time......

 

But I not only change their contents, but also change their properties, such as color, font and so on. Therefore, the fundamental question is, how to operate multiple characters in one text box at a time? All characters can use "textrange" attribute directly, and a single character can use "textranges" or "characters" attribute (index), but multiple characters don't know how to deal with it, or there are other object attributes?

Of course, I think of a compromise to deal with it, that is, to do a loop to enumerate all the characters, and then add conditions to deal with the characters to be processed. However, this is a very unwise method, and the execution speed is also very slow. Therefore, if there are any attributes or methods that can directly realize the processing of multiple characters at one time in a text box, it is best!

In addition, when changing the content with the "replace" method, all the same characters will be replaced, but if I only want to change some of them, I can't do it with the "replace" method, such as changing "123123123" to "123abc123123".

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines