Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-11-2022, 05:10 AM
kamkwok6 kamkwok6 is offline Split the heading number and text Mac OS X Split the heading number and text Office 2021
Novice
Split the heading number and text
 
Join Date: Mar 2022
Posts: 3
kamkwok6 is on a distinguished road
Default Split the heading number and text

In the document, all lines are in "Heading" style


Sample question .docx
Sample question.jpg
I am looking for a single click to split my selection into heading number, and put the "heading text" into next line.

After that, remain the "heading number" in "Heading" style
and change the 'heading text" to "Normal" Style.

Final outcome as below, Many thanks!!!
Sample (Final Outcome).docx
Reply With Quote
  #2  
Old 03-11-2022, 06:26 AM
gmayor's Avatar
gmayor gmayor is offline Split the heading number and text Windows 10 Split the heading number and text Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following should work, based on your example:
Code:
Sub Macro1()
Dim oRng As Range
Dim oPara As Paragraph
Dim i As Integer
    If Len(Selection.Range) = 0 Then
        MsgBox "Nothing selected", vbCritical
        Exit Sub
    End If
    For i = Selection.Paragraphs.Count To 1 Step -1
        Set oPara = ActiveDocument.Paragraphs(i)
        If oPara.Range.Style Like "Heading #" Then
            Set oRng = oPara.Range
            oRng.Collapse 0
            oRng.MoveEndUntil Chr(9)
            oRng.End = oRng.End + 1
            oRng.Collapse 0
            oRng.Text = vbCr
            Set oRng = oRng.Next.Paragraphs(1).Range
            oRng.Style = "Normal"
        End If
    Next i
lbl_Exit:
    Set oPara = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
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 03-12-2022, 08:22 PM
kamkwok6 kamkwok6 is offline Split the heading number and text Mac OS X Split the heading number and text Office 2021
Novice
Split the heading number and text
 
Join Date: Mar 2022
Posts: 3
kamkwok6 is on a distinguished road
Default

Thank you for the reply, however I still have some question when using your code.

When I select "7.23.7.7" and then run your code.
Sample question2.jpg

The effect was made on "7.11.7"
Sample question3.jpg
Reply With Quote
  #4  
Old 03-12-2022, 09:39 PM
gmayor's Avatar
gmayor gmayor is offline Split the heading number and text Windows 10 Split the heading number and text Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

OK, try the following instead
Code:
Sub Macro1()
Dim oRng As Range
Dim oPara As Paragraph
    If Len(Selection.Range) = 0 Then
        MsgBox "Nothing selected", vbCritical
        Exit Sub
    End If
    For Each oPara In Selection.Paragraphs
        If oPara.Range.Style Like "Heading #" Then
            Set oRng = oPara.Range
            oRng.Collapse 1
            oRng.MoveEndUntil Chr(9)
            oRng.End = oRng.End + 1
            oRng.Collapse 0
            oRng.Text = vbCr
            Set oRng = oRng.Next.Paragraphs(1).Range
            oRng.Style = "Normal"
        End If
    Next oPara
lbl_Exit:
    Set oPara = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
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
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting heading number AFTER heading text Aunt Pauline Word 1 08-29-2019 03:24 PM
Split the heading number and text Mail Merge - split merged documents and rename each split document based on text in header FuriousD Word VBA 1 05-12-2019 04:06 AM
Split the heading number and text How to add space between heading number and heading text Dr Wu Word 4 07-15-2018 03:52 AM
Split the heading number and text xref to heading 5.1 stubbornly displays 0 as heading number eNGiNe Word 4 06-17-2018 11:05 PM
Control the size of space between heading number and heading text Dr Wu Word 1 07-17-2013 12:24 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:27 PM.


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