Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-30-2021, 12:45 AM
Thorin23 Thorin23 is offline Assign different heading styles for different paragraphs Windows 10 Assign different heading styles for different paragraphs Office 2016
Novice
Assign different heading styles for different paragraphs
 
Join Date: Apr 2021
Posts: 4
Thorin23 is on a distinguished road
Default Assign different heading styles for different paragraphs

Good morning all,



I allow myself to ask your help for a problem which, despite my research on the internet, does not find answers.
I am introducing you my problem.

I export from Excel a succession of paragraphs to a single bookmark of an openned Word document. These different paragraphs are structured as follows (illustrative text):

A. Getting started
(1). Please turn on your PC
(2). Please wait for full boot
(a). Option 1: ...
(b). Option 2: ...
B. Parameterization
(1). etc ...
(2). etc ...
(3). etc ....

Then, I wanted to know if it was possible to assign to each paragraph (A. ...; (1). ...; (a). ...) a heading style that I had already configured in my Word document (heading style without numbering because they are already present during export).

I had started writing this piece of code:
Code:
Dim Paragraph As Word.Paragraph
 
Selection.GoTo What:=wdGoToBookmar, name:="Z1"
With Selection
     For each Paragraph in Selection.Paragraphs
        Paragraphe.Style = ActiveDocument.Styles("ZStyle(1)")
     Next
End with
But this code will assign the same heading style to all paragraphs of my text. The problem is that I can't tell to Word (I mean in VBA)
- If the paragraph begins with "A." or "B." etc ...; assign it heading style 1
- If the paragraph begins with "(1)." or "(2)." etc ...; assign it heading style 2
- If the paragraph begins with "(a)." or "(b)." etc ...; assign it heading style 3

In addition, there are may be empty lines between two paragraphs (I mean end of paragraph marks) which of course I do not want to assign a heading style.

I hope I managed to be understandable. Do not hesitate to tell me if more precision is needed.

I thank in advance all people who can help me.

Have a good day
Reply With Quote
  #2  
Old 04-30-2021, 09:23 PM
gmayor's Avatar
gmayor gmayor is offline Assign different heading styles for different paragraphs Windows 10 Assign different heading styles for different paragraphs Office 2019
Expert
 
Join Date: Aug 2014
Posts: 3,446
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

If the start texts are typed text and not outline numbers, the following will work
Code:
Sub Macro1()
'Graham Mayor - https://www.gmayor.com - Last updated - 01 May 2021 
Dim oPara As Word.Paragraph
Dim oRng As Range
Dim vChar1 As Variant, vChar2 As Variant, vChar3 As Variant
Dim iList As Integer

Const strList1 As String = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
Const strList2 As String = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
Const strList3 As String = "0,1,2,3,4,5,6,7,8,9"

    vChar1 = Split(strList1, ",")
    vChar2 = Split(strList2, ",")
    vChar3 = Split(strList3, ",")

    If ActiveDocument.Bookmarks.Exists("Z1") = True Then
        Set oRng = ActiveDocument.Bookmarks("Z1").Range
        For Each oPara In oRng.Paragraphs
            For iList = 0 To UBound(vChar1)
                If oPara.Range.Characters(1) = vChar1(iList) And _
                   oPara.Range.Characters(2) = Chr(46) Then
                    oPara.Style = "Heading 1"
                    GoTo Skip
                End If
            Next iList
            For iList = 0 To UBound(vChar2)
                If oPara.Range.Characters(2) = vChar2(iList) And _
                   oPara.Range.Characters(1) = Chr(40) Then
                    oPara.Style = "Heading 3"
                    GoTo Skip
                End If
            Next iList
            For iList = 0 To UBound(vChar3)
                If oPara.Range.Characters(2) = vChar3(iList) And _
                   oPara.Range.Characters(1) = Chr(40) Then
                    oPara.Style = "Heading 2"
                    GoTo Skip
                End If
            Next iList
Skip:
        Next oPara
    End If
    Set oRng = Nothing
    Set oPara = Nothing
End Sub
If they are outline numbers then associate the styles with the outline levels.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-30-2021, 11:07 PM
Guessed's Avatar
Guessed Guessed is offline Assign different heading styles for different paragraphs Windows 10 Assign different heading styles for different paragraphs Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 2,324
Guessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to behold
Default

I often find the Autoformat command to be useful for this type of task.

What happens to your document styling if you press Ctrl-Alt-K
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 05-02-2021, 06:22 AM
Thorin23 Thorin23 is offline Assign different heading styles for different paragraphs Windows 10 Assign different heading styles for different paragraphs Office 2016
Novice
Assign different heading styles for different paragraphs
 
Join Date: Apr 2021
Posts: 4
Thorin23 is on a distinguished road
Default

Hello everyone,

First, thank you very much for your help and your involvement for this problem.

Concerning your code @gmayor, I tried it and add two lines :
Code:
If oPara.range.characters(1) = Chr(13) then 
GoTo Skip
End if
and this works wonderfully

So a huge thank you for that ! Now, I'm going to read each line of your code in order to understand and reuse them for a futur project.

I also tried the shortcut : Ctrl-Alt-K
And this one works but not as expected for my document which have a particular set-up.

Thank you again for your help and all the best !
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting all paragraphs based on pre defined styles afif Word VBA 0 07-26-2019 06:18 AM
Add a heading for each paragraph depending on the previous paragraphs numbering herman Word 4 07-15-2019 09:48 PM
Assign different heading styles for different paragraphs Heading and the paragraphs grouped together in MS Word 2007 Volle13 Word 8 12-05-2016 06:14 PM
Assign different heading styles for different paragraphs Styles: Heading 4 stuck at same heading number Hallet Word 1 05-31-2012 02:37 PM
Assign different heading styles for different paragraphs How to have two styles for heading 1? Jamal NUMAN Word 3 07-24-2011 04:33 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 01:40 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft