Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-04-2024, 05:28 AM
RRB's Avatar
RRB RRB is offline Formatting text in VBA Windows 11 Formatting text in VBA Office 2021
Susan Flamingo
Formatting text in VBA
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default Formatting text in VBA

Hi friends!!



Little by little with everybody's help I am starting to realize what and how to do some things in VB and what I cannot do (or at least should attempt to do

Can you format a text string BEFORE it is inserted within the doc? I mean it doesn't "exist" yet.

If If I have a string of text "Have a good day" and want to format it with the char style "MyCharStyle" AND THEN insert it somewhere, how would that be done?

Thank you and have a good day!

Susan Flamingo
Reply With Quote
  #2  
Old 03-04-2024, 06:51 AM
Italophile Italophile is offline Formatting text in VBA Windows 11 Formatting text in VBA Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

That isn't possible because strings only contain text, not formatting. That is why the Range object contains two properties, Text, a string with no formatting, and FormattedText, which does as its name suggests.

To apply a character style you need to insert the text into the document, then apply the style.
Reply With Quote
  #3  
Old 03-04-2024, 07:08 AM
gmaxey gmaxey is online now Formatting text in VBA Windows 10 Formatting text in VBA Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

As Italophile explains. No.

Why? What difference does it make if it is formatted before or after if the net result is formatted?


You could use:
Code:
Sub Demo()
  InsertStringAsFormattedText "Have a good day ", "MyCharStyle"
End Sub
Sub InsertStringAsFormattedText(strText, strStyle)
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = Selection.Range
  oRng.Text = strText
  oRng.Style = strStyle
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting text in VBA Text Wrapping Without a Table or Text-box (Formatting Assistance) COWNoah Word 8 08-08-2023 02:55 PM
Replacing a comment with underline or other formatting (or adding that formatting to commented text) paulkaye Word 4 05-16-2016 05:38 AM
Formatting text in VBA The Logic Behind Apply Formatting of Surrounding Text in Reveal Formatting Pane Instructor Mark Word 4 04-06-2016 01:32 PM
Formatting text in VBA text formatting kjared7 PowerPoint 1 12-26-2014 07:42 PM
Formatting text in VBA Formatting- Apply changes to highlighted text results in same change to other text sential Word 6 01-10-2014 03:22 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:48 PM.


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