#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
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. |
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
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?
|
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 |
help with citations styles, xsl file format | LMHmedchem | Word | 2 | 10-22-2014 10:40 AM |
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 |