Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-16-2024, 11:59 PM
RRB's Avatar
RRB RRB is offline Strugling with a macro Windows 11 Strugling with a macro Office 2021
Susan Flamingo
Strugling with a macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default Strugling with a macro

Hi friends! Hope you are all well!

I am struggling to write a macro and have not been successful

I have certain paragraphs that follow the same syntax. At their beginning are codes for the pagination program that are in curly brackets like {020).

Then there is a sentence that ends with a period. Then the rest of the paragraph. The paragraph has to be formatted with the style MyParStyle. The pagination codes have to be formatted with MyPgSTyle (a character style) and the first sentence up to and including the period has to be formatted with a char style "MySentStyle".

Assuming we are presently at the end of the paragraph how would a macro handle this?

All assistance is sincerely appreciated!

Thank you and have a good day!



Susan Flamingo
Reply With Quote
  #2  
Old 03-17-2024, 02:24 AM
Guessed's Avatar
Guessed Guessed is offline Strugling with a macro Windows 10 Strugling with a macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This code works for me and shows how you might do it with your custom styles. I've used built-in styles for illustrating the method. Note that VBA considers '{020)' to be three words which is why the code applies the last character style to the first three words.
Code:
Sub FormatPara()
  Dim aPar As Paragraph, aRng As Range
  Set aPar = Selection.Paragraphs(1)
  With aPar
    .Style = "Body Text"      'Apply a paragraph style to the entire paragraph
    .Range.Sentences(1).Style = "Emphasis"    'Apply a character style to the first sentence
    Set aRng = .Range.Words(3)        'Define a Range for the third 'word'
    aRng.Start = .Range.Start         'Extend that range to the start of the paragraph
    aRng.Style = "Strong"             'Style the range
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 03-17-2024, 02:45 AM
RRB's Avatar
RRB RRB is offline Strugling with a macro Windows 11 Strugling with a macro Office 2021
Susan Flamingo
Strugling with a macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

Works perfectly. Your assistance is sincerely appreciated. If I can ever help with anything (what could that be? don't hesitate to reach out.
Reply With Quote
  #4  
Old 03-17-2024, 05:14 AM
gmaxey gmaxey is offline Strugling with a macro Windows 10 Strugling with a macro 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

Just be aware that Word does not think of a sentence the same way as you or I. Consider:
{20} Mr. Smith goes to Washington. This is the rest of the paragraph text.
__________________
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
Macro to Remove Paras with Line Spac 6; Macro to Convert Paragraphs to Outline Numbered venganewt Word VBA 0 01-25-2022 06:28 PM
Calling macro from loop-all-files macro only opens one file Peterson Word VBA 0 03-27-2021 07:58 PM
Strugling with a macro Footnote extraction macro [Why is this macro so slow? / anyway to make it faster?] Le_Blanc Word VBA 10 03-22-2021 11:38 AM
Spell check macro within macro button field doesn't work in one document samuelle Word VBA 0 07-20-2016 02:27 AM
custom icon, undo/redo for macro, permanent macro Rapier Excel 0 08-05-2013 06:30 AM

Other Forums: Access Forums

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