Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2021, 07:26 PM
Bikram Bikram is offline Format text between two styles Windows 10 Format text between two styles Office 2007
Advanced Beginner
Format text between two styles
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Post Format text between two styles

I am beginner in vba hoping for some guidelines to automate my daily task. So, any help would be greatly appreciated.

Here is my code

Sub find_and_format()


Dim doc As Document
Dim rng As Range


Set doc = Application.ActiveDocument
Set rng = doc.Content

With rng.find
.ClearFormatting
.Text = "Answer"
.Style = ActiveDocument.Styles("ans")
.Format = False
.MatchAllWordForms = False
.MatchWildcards = False
.MatchCase = False
.Wrap = wdFindContinue
End With


Do Until rng.find.Execute = False
Selection.MoveDown unit:=wdParagraph, Count:=1
Selection.Style = ActiveDocument.Styles("t")
Selection.MoveDown unit:=wdParagraph, Count:=1
'do while manual numbering is found
Do While Selection.Range.ListFormat.ListType = wdListSimpleNumbering
' and then i want to select and change the autonumbering to manual numbering
Selection.Range.ListFormat.ConvertNumbersToText
Selection.Style = ActiveDocument.Styles("tt")

' or repeat this process until another style is found
Loop
'can i access the range between a style to another style and then format it??


Loop

'i have attached a sample file



End Sub
Attached Files
File Type: docx Sample.docx (21.3 KB, 9 views)
Reply With Quote
  #2  
Old 08-08-2021, 05:29 PM
Guessed's Avatar
Guessed Guessed is offline Format text between two styles Windows 10 Format text between two styles Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

If you are processing almost every paragraph I don't see the usefulness of using the Find command. I note that your sample has two input lists but only one of them was an autolist. This code is set up to recognise a hard-coded list by the presence of ".tab" so it works with your sample but may not work with other lists if they don't use that pattern
Code:
Sub SetupQuiz()
  Dim iParaNum As Long, aRng As Range, aPara As Paragraph
  Set aRng = ActiveDocument.Range  'or Selection.Range
  For iParaNum = aRng.Paragraphs.Count To 1 Step -1
    Set aPara = aRng.Paragraphs(iParaNum)
    aPara.Range.Select
    Select Case aPara.Style
      Case "ans", "Q"
        'do nothing
      Case Else
        If aPara.Range.ListFormat.ListType = wdListNoNumbering Then
          aPara.Style = "t"
          If aPara.Range.Text Like "*." & vbTab & "*" Then aPara.Style = "tt"
        Else
          aPara.Range.ListFormat.ConvertNumbersToText
          aPara.Style = "tt"
        End If
    End Select
  Next iParaNum
End Sub
Note that the code has to work backwards to avoid autonumbered list items renumbering if the preceding paragraph is converted before you process the next paragraph.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-08-2021, 08:04 PM
Bikram Bikram is offline Format text between two styles Windows 10 Format text between two styles Office 2007
Advanced Beginner
Format text between two styles
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Post

Thank you guessed for the reply. It works great. Sorry, I should have explained fully in the beginning about the document i am working at. I was using find because i am working on a book and it have many styles and format. so by using find i was trying to find a style or text and then run a do loop until another style is met. By doing so i think that
will be able to filter paragraphs.
I have styles applied throughout the book and i want to use those style as divider or mark which word can recognize and format instances between those styles separately because both exercise and text part have "*". & vbtab & "*" but they have to be formatted differently or with different styles. I have attached a sample file plz have a look and suggest effective method for the problem.
Attached Files
File Type: docx Sample.docx (25.9 KB, 7 views)
Reply With Quote
  #4  
Old 08-08-2021, 09:05 PM
Guessed's Avatar
Guessed Guessed is offline Format text between two styles Windows 10 Format text between two styles Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

That sample doesn't contain the word 'Answer' which was your original trigger. I have no idea on what you want coded in that sample.

In principle, I would recommend you work out how to compartmentalise the question/answers in some way and then iterate through those repeating parts for your other code. For instance, you could put them into Rich Text Content Controls and then iterate through those CCs to process the text.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 08-08-2021, 11:39 PM
Bikram Bikram is offline Format text between two styles Windows 10 Format text between two styles Office 2007
Advanced Beginner
Format text between two styles
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Post

Sorry sir, for confusing you. What i want to be coded is to access the range between two styles and select that range and hardcode them according to their formatting needs. Sir, can you please, show me how to access the range or set the range between two paragraph styles?
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Styles in Templates to help format text - Importing and working with Templates daithy Word 2 01-03-2020 05:06 PM
Format text between two styles help with citations styles, xsl file format LMHmedchem Word 2 10-22-2014 10:40 AM
Format text between two styles Problem with using Styles to format text. cummins Word 3 08-16-2012 05:34 AM
Styles format not permanent Zuca Word 1 07-02-2010 10:02 PM
Creating/editting citation format styles shark0807 Word 1 10-01-2008 07:12 AM

Other Forums: Access Forums

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