简体   繁体   English

VBA Word 在标题级别/章节中查找具有特殊样式的文本

[英]VBA Word find text with a special style in heading level/chapter

EDIT编辑
Okay first thank you for all the messages, I'll try to explain again what exactly I'm doing and looking for好的,首先感谢您的所有消息,我会尝试再次解释我到底在做什么和寻找什么
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..)在整个文档中,我需要在每章的预定义 excel 中编写数据(标题级别 1 - 4),有发现(带项目符号样式的文本)所以如果本章有发现,我必须看多少次我可以按编号写在excel,继续下一章举例(希望比上一章好..)
Required chapters start with 3. ( headings level 1 )所需章节以 3 开头。(一级标题)
3. Ü3 3. Ü3
any text任何文本
3.1 Ü3.1 3.1 Ü3.1
any text任何文本
3.1.1 Ü3.1.1 3.1.1 Ü3.1.1
any text任何文本
3.1.2 Ü3.1.2 3.1.2 Ü3.1.2
any text任何文本
3.1.2.1 Ü3.1.2.1 3.1.2.1 Ü3.1.2.1
• Text with bulleted style > I searching • 带项目符号样式的文本 > I 搜索
• Text with bulleted style > I searching • 带项目符号样式的文本 > I 搜索
3.1.2.2 Ü3.1.2.2 3.1.2.2 Ü3.1.2.2
any text任何文本
4. Ü4 4. Ü4
any text任何文本
4.1 Ü4.1 4.1 Ü4.1
• Text with bulleted style > I searching • 带项目符号样式的文本 > I 搜索
5. Ü5 5. Ü5
5.1 Ü5.1 5.1 Ü5.1
5.2 Ü5.2 5.2 Ü5.2
• Text with bulleted style > I searching • 带项目符号样式的文本 > I 搜索
6. Ü6 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这意味着在上面的示例中,第 3 章 (3. - 3.1.2.1) 2x 文本以项目符号样式出现 3.1.2.2 我可以忽略,因为在 3.1.2.1 中,我正在寻找的文本已经出现意味着我必须写入excel
2x 2倍
Ü3 in column c3/c4 c3/c4 列中的 Ü3
Ü3.1 in column d3/4 d3/4 列中的 Ü3.1
Ü3.1.2 in column e3/4 e3/4 栏 Ü3.1.2
and most important the headline level where the text occurs Ü3.1.2.2 in column f3/4最重要的是文本出现在 Ü3.1.2.2 列 f3/4 的标题级别
thereafter to the next chapter 4.此后进入下一章 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它发生了多少次(我必须在 excel 中编写它的次数)以及它发生在什么级别,因为我必须在 excel 中拥有它
Should then look like this in excel然后应该在 excel 中看起来像这样
am grateful for any help... image attached below感谢您的帮助...下面附上图片

how it should look in excel then那么它在 excel 中应该是什么样子

'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

It doesn't look like Word can find a character/paragraph style embedded in a paragraph. Word 似乎无法找到段落中嵌入的字符/段落样式。 ie 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.即,如果我有一段标题 1 样式的文本,我将该标题的一个词格式化为正文文本 3(组合字符/段落样式)并添加了斜体属性,我可以看到该词是斜体但 Bofy Text 3找不到样式。

However Word can find one or more font attributes for the 'Body Text 3' style, specifically in this case the italic text.但是,Word 可以为“Body Text 3”样式找到一个或多个字体属性,特别是在本例中为斜体文本。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM