-1

In the whole doc are data that I need to write in a predefined excel per chapter (headings level 1 - 4) there are findings (text with bulleted style) so if there is a finding in this chapter i have to look how many times so i can write it in excel according to the number, and continue to the next chapter as an example ( hopefully better than the last one ..)
Required chapters start with 3. ( headings level 1 )
3. Ü3
any text
3.1 Ü3.1
any text
3.1.1 Ü3.1.1
any text
3.1.2 Ü3.1.2
any text
3.1.2.1 Ü3.1.2.1
• Text with bulleted style > I searching
• Text with bulleted style > I searching
3.1.2.2 Ü3.1.2.2
any text
4. Ü4
any text
4.1 Ü4.1
• Text with bulleted style > I searching
5. Ü5
5.1 Ü5.1
5.2 Ü5.2
• Text with bulleted style > I searching
6. Ü6
This would mean with the example above that in chapter 3 (3. - 3.1.2.1) 2x text occurs with bulleted style 3.1.2.2 I can ignore because in 3.1.2.1 the text I am looking for already occurs means I have to write in excel
2x
Ü3 in column c3/c4
Ü3.1 in column d3/4
Ü3.1.2 in column e3/4
and most important the headline level where the text occurs Ü3.1.2.2 in column f3/4
thereafter to the next chapter 4.
so in this chapter is this text, yes? how many times does it occur (the number of times i have to write it in excel) and what level does it occur because i have to have that in excel
Should then look like this in excel

how it should look in excel then

'code I use for the remaining columns
Public Sub exportToExcel()

Const strTemplateName As String = "check-doc.xlsm"
Dim doc As Document, cc As ContentControl
Dim strFolder As String
Dim counterForMeasures As Integer
Dim counterForFindings As Integer
Dim counterForHeading1 As Integer
Dim g As Integer, a As Integer, b As Integer, c As Integer, d As Integer, e As Integer, f As Integer, h As Integer, i As Integer, priorityPlaceholder
Dim strAutidNr As String
Dim arrSplitStrAuditNr() As String
Dim strdate1 As String
Dim strdate2 As String
Dim arrSplitDate() As String
Dim MonthsDE As String
Dim MonthsEN As String
Dim arrMonthsDE() As String
Dim arrMonthsEN() As String
MonthsDE = "Januar Februar März April Mai Juni Juli August September Oktober November Dezember"
MonthsEN = "January February March April May June July August September October November December"
arrMonthsDE = Split(MonthsDE, " ")
arrMonthsEN = Split(MonthsEN, " ")
Dim cr2 As String
Dim xlwb As Excel.Workbook, xlApp As Excel.Application
Dim xlwsh As Excel.Worksheet


Set doc = ThisDocument
strFolder = ActiveDocument.AttachedTemplate.Path & Application.PathSeparator & strTemplateName

If Not MyFileExists(strFolder) Then
MsgBox strFolder, vbInformation, "Template does not exist"
Exit Sub
End If
Call UnlockAllCC ' sperre lösen

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlwb = xlApp.Workbooks.Add(Template:=strFolder)
Set xlwsh = xlwb.Worksheets("Tabelle1")

'M count
counterForMeasures = 2 ' header berücksichtigen
For Each cc In ActiveDocument.ContentControls
If cc.Tag = "cc_TextMaßnahme" Then
counterForMeasures = counterForMeasures + 1
End If
Next cc

' bulleted style count
counterForFindings = 2 ' header berücksichtigen
For Each cc In ActiveDocument.ContentControls
If cc.Tag = "cc_eineFeststellung" Then
counterForFindings = counterForFindings + 1
End If
Next cc

' Heading1 count// cc_Heading1
counterForHeading1 = 2 ' header berücksichtigen
For Each cc In ActiveDocument.ContentControls
If cc.Tag = "cc_Heading1" Then
counterForHeading1 = counterForHeading1 + 1
End If
Next cc



'a = 3 ' Datum
For a = 3 To counterForFindings
For Each cc In ActiveDocument.ContentControls

If cc.Tag = "cc_DatumRevisionsbericht" Then

If cc.Range.Text <> "Klicken oder tippen Sie, um ein Datum einzugeben." And cc.Range.Text <> "Click or tap to enter a date." Then
cc.LockContents = False

If cc.Range.Text Like "*.*" Then
arrSplitDate = Split(cc.Range.Text, ".")
'strdate1 = arrSplitDate(0)
strdate2 = arrSplitDate(1)
arrSplitDate = Split(strdate2, " ")
strdate1 = arrSplitDate(2)
strdate2 = arrSplitDate(1)
If strdate2 = arrMonthsEN(0) Or strdate2 = arrMonthsDE(0) Then
strdate2 = "01"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(1) Or strdate2 = arrMonthsDE(1) Then
strdate2 = "02"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(2) Or strdate2 = arrMonthsDE(2) Then
strdate2 = "03"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(3) Or strdate2 = arrMonthsDE(3) Then
strdate2 = "04"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(4) Or strdate2 = arrMonthsDE(4) Then
strdate2 = "05"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(5) Or strdate2 = arrMonthsDE(5) Then
strdate2 = "06"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(6) Or strdate2 = arrMonthsDE(6) Then
strdate2 = "07"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(7) Or strdate2 = arrMonthsDE(7) Then
strdate2 = "08"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(8) Or strdate2 = arrMonthsDE(8) Then
strdate2 = "09"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(9) Or strdate2 = arrMonthsDE(9) Then
strdate2 = "10"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(10) Or strdate2 = arrMonthsDE(10) Then
strdate2 = "11"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
If strdate2 = arrMonthsEN(11) Or strdate2 = arrMonthsDE(11) Then
strdate2 = "12"
xlwsh.Range("A" & a).Value = strdate1 & " " & strdate2
End If
End If
End If
End If

Next cc
Next a


'b = 3 ' Gep einheit -
strAutidNr = GetNr(ActiveDocument)
If strAutidNr Like "*_*" Then
arrSplitStrAuditNr = Split(strAutidNr, "_")

For b = 3 To counterForFindings
xlwsh.Range("B" & b).Value = arrSplitStrAuditNr(1)

Next b
End If







'c = 3 ' h1



'd = 3 ' h2


'e = 3 ' h3


'f = 3 ' h4


g = 3 ' bulleted style
For Each cc In ActiveDocument.ContentControls

If cc.Tag = "cc_eineFeststellung" Then
cc.LockContents = False
xlwsh.Range("G" & g).Value = cc.Range.Text
If g = counterForFindings Then
Exit For
End If
g = g + 1
End If
Next cc


h = 3 ' M
For Each cc In ActiveDocument.ContentControls

If cc.Tag = "cc_TextMaßnahme" Then
cc.LockContents = False
xlwsh.Range("H" & h).Value = cc.Range.Text
If h = counterForMeasures Then
Exit For
End If
h = h + 1
End If

Next cc

i = 3 ' priorität
For Each cc In ActiveDocument.ContentControls

If cc.Tag = "cc_Nr" Then
cc.LockContents = False
priorityPlaceholder = Left(cc.Range.Text, 1)

xlwsh.Range("I" & i).Value = priorityPlaceholder
If i = counterForMeasures Then
Exit For
End If
i = i + 1
End If

Next cc

' close obj instancen
Set xlwb = Nothing
Set xlApp = Nothing
Set xlwsh = Nothing
Set doc = Nothing

Call LockAllCC ' sperre setzen
End Sub
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • You question does not clearly explain what you are looking for. Are your headings in a heading style. What is different about the special style compared to the heading style? – freeflow Jan 24 '23 at 12:34
  • Yes my headings are in a heading style and the text I'm looking for also has a style that I use with style sheets – KnechtenP99 Jan 24 '23 at 13:51
  • all have their own style, heading1 = heading1 -style heading2 = heading2 -style heading3 = heading3-style heading4 = heading4-style normal-text = standard-style text i looking for = bullet-style – KnechtenP99 Jan 24 '23 at 14:20
  • What is the style shown for headings when you look at your document in draft mode. In draft mode you should see a column at the left of the document which shows the style for each paragraph. – freeflow Jan 24 '23 at 14:30
  • found it.. as a example it shows.. Überschrift1 3. Ü3 Überschrift2 3.1 Ü3.1 Überschrift3 3.1.1 Ü3.1.1 Überschrift4 3.1.1.1 Ü3.1.1.1 Aufzählung text i looking (bullet-style) – KnechtenP99 Jan 24 '23 at 14:46
  • So you have paragraphs which are a heading style and you are looking for the style Aufzählung which is contained within the heading text? If Aufzählung is a style what type of style is it. Character, Paragraph or combined Character/Paragraph – freeflow Jan 24 '23 at 15:23
  • Thanks first for all the questions, I have attached a picture and tried again to explain it exactly – KnechtenP99 Jan 25 '23 at 09:26
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://meta.stackexchange.com/q/5221) – Adriaan Feb 02 '23 at 10:58
  • okay i'm sorry, thought i was confusing people with too much – KnechtenP99 Feb 02 '23 at 12:39

2 Answers2

0

It doesn't look like Word can find a character/paragraph style embedded in a paragraph. i.e if I have a paragraph of text in Heading 1 style and I format a word of that heading as Body Text 3 (a combined character/paragraph style) with italic attribute added, I can see that the word is italic but the Bofy Text 3 style can't be found.

However Word can find one or more font attributes for the 'Body Text 3' style, specifically in this case the italic text.

The following code may be of help

Option Explicit


Sub test()

    ' Body Text 3 has also had the italic formatting added to the style.
    CountStyleInHeadings ("Body Text 3")
End Sub
Public Function CountStyleInHeadings(ByVal ipStylename As String) As Variant

    Dim myCounts As Variant
    ReDim myCounts(1 To 9)
    
    Dim myPara As Variant
    For Each myPara In ActiveDocument.StoryRanges(wdMainTextStory).Paragraphs
    
        Dim myRange As Range
        Set myRange = myPara.Range
        myRange.Select
        If myPara.OutlineLevel <> wdOutlineLevelBodyText Then
        
            If StyleNameFound(myRange, ipStylename) Then
            
                myCounts(myPara.OutlineLevel) = myCounts(myPara.OutlineLevel) + 1
                
            End If
            
        End If
            
    Next
    
End Function


Public Function StyleNameFound(ByRef ipParagraph As Range, ByRef ipStylename As String)
    Debug.Print ipParagraph.Text
    Debug.Print ipStylename
    With ipParagraph.Find
    
        .ClearFormatting
        .Font.Italic = True
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
    End With
    
    Debug.Print ipParagraph.Find.Found
    StyleNameFound = ipParagraph.Find.Found
End Function

freeflow
  • 4,129
  • 3
  • 10
  • 18
  • The question is very poorly worded but I think you have misunderstood. My understanding is that the OP wants to find how many times the bulleted style has been used within a heading level, not within a heading paragraph. – Timothy Rylatt Jan 24 '23 at 18:05
  • exactly, should run through all headings, it can happen that a heading level occurs more than once, but I should only write the heading level in the excel where this bullet style occurs in it, so 4. 4.1 4.2 Ü4.2 2x bulleted style in headings level 2 2x bulleted style occurred, > write Ü4.2 2x in excel then continue 5. 5.1 5.2 Ü5.2 1x bulleted style in headings level 2 1x bulleted style occurred, > Ü5.2 1x write into excel ---------- maybe comments how i could adjust my text above so that other people understand me faster what i mean – KnechtenP99 Jan 24 '23 at 18:37
  • as also said above i found something, but it stops in the first chapter (3.) (chapters = heading level1) and does not go further but it has found the bulleted style in 3.1.1.1 is there a way to customize it so that it goes through all chapters ( 3. / 4. / 5. etc. ) and shows me the heading level where the bulleted style occurs and how often? – KnechtenP99 Jan 24 '23 at 18:49
  • With ActiveDocument.Range With .Find .Text = "" .Format = True .Style = "bulleted style" .Forward = True .Wrap = wdFindStop End With Do While .Find.Execute Set rngÜ = .Paragraphs(1).Range ' as range Set rngÜ = rngÜ.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") Debug.Print rngÜ.Text Loop End With – KnechtenP99 Jan 24 '23 at 18:49
  • OK, so if I now understand correctly the OP wishes to find all occurrences of bulleted text between two heading paragraphs and attribute the count of the found instances to the heading level at the start heading level. So if we had Heading level 1, 5 instances of bulleted text then heading level 2 then 4 instances of bulleted text then heading levels 3, the counts would be 5 for heading level 1 and four for heading level 2. Is this correct? – freeflow Jan 24 '23 at 21:09
  • Thanks first for all the questions, I have attached a picture and tried again to explain it exactly – KnechtenP99 Jan 25 '23 at 09:22
0

Here's some code to get you started. It returns the heading level, heading text & bullet text for each paragraph in the 'Bullet' style.

Sub GetBulletHeadings()
Application.ScreenUpdating = False
Dim RngHd As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Style = "Bullet"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = False
  End With
  Do While .Find.Execute
    Set RngHd = .Paragraphs(1).Range.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
    MsgBox Right(RngHd.Paragraphs.First.Range.Style, 1) & vbCr & RngHd.Paragraphs.First.Range.Text & vbCr & .Text
    .Collapse wdCollapseEnd
  Loop
End With
Set RngHd = Nothing
Application.ScreenUpdating = True
End Sub
macropod
  • 12,757
  • 2
  • 9
  • 21
  • First of all, thank you, but unfortunately it stops in chapter 3 and tells me that it happens but not how often. have attached a picture above how it should look and again tried to explain it better – KnechtenP99 Jan 25 '23 at 09:28
  • I was wrong, you were right, it goes through all the headings and shows me the heading level where it occurs. thanks is really a good start – KnechtenP99 Jan 25 '23 at 11:02