【问题标题】:How do I extract instances of Bold text from all open Word documents如何从所有打开的 Word 文档中提取粗体文本的实例
【发布时间】:2021-12-26 18:20:39
【问题描述】:

您好,以下代码从活动 Word 文档中提取所有粗体文本实例,并将其复制到新创建的 Word 文档中。

谁能帮我调整代码以对所有打开的 Word 文档执行相同的任务到新创建的 Word 文档中。

非常感谢任何帮助。

Sub A__GrabTheBolds()
On Error GoTo cleanUp
Application.ScreenUpdating = False
     Dim ThisDoc As Document
     Dim ThatDoc As Document
     Dim r As Range
     Set ThisDoc = ActiveDocument
     Set r = ThisDoc.Range
     Set ThatDoc = Documents.Add
     
     With r
         With .Find
             .Text = ""
             .Format = True
             .Font.Bold = True
         End With
         Do While .Find.Execute(Forward:=True) = True
            'If r.HighlightColorIndex = wdDarkYellow Then 'highlightcols(7)
            If r.Bold Then
               ThatDoc.Range.Characters.Last.FormattedText = .FormattedText
               ThatDoc.Range.InsertParagraphAfter
               .Collapse 0
            End If
          Loop
     End With
cleanUp:
Application.ScreenUpdating = True
Set ThatDoc = Nothing
Set ThisDoc = Nothing
End Sub

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    您可以使用 Documents-collection 返回所有打开的文档:

    
    Sub A__GrabTheBolds()
    On Error GoTo cleanUp
    Application.ScreenUpdating = False
         Dim ThisDoc As Document
         Dim ThatDoc As Document
         Dim r As Range
         
         Set ThatDoc = Documents.Add
    
         'iterate over all open word documents
         'For Each ThisDoc In Application.Documents
    
          'handle documents in the order they were opened
          'reverse order of documents collection
          'loop until second to last as last one is ThatDoc
    
    
          Dim i As Long
          
          Dim FileNames As String, fFound As Boolean
          Dim fWritten As Boolean
          
          For i = Application.Documents.Count To 2 Step -1
            Set ThisDoc = Application.Documents(i)
            
            'Don't check document where the code runs
            If Not ThisDoc Is ThisDocument Then
            
        
                Set r = ThisDoc.Range
                
                With r
                    With .Find
                        .Text = ""
                        .Format = True
                        .Font.Bold = True
                    End With
                    
                    Do While .Find.Execute(Forward:=True) = True
                    
                        '<-- remove this part if not needed
                        
                        'add filename if the first bold range
                        If fWritten = False Then
                            ThatDoc.Content.InsertAfter vbCrLf & vbCrLf & ThisDoc.Name & vbCrLf
                        End If
                        'remove this part if not needed -->
                        
                        fWritten = True
                        
                       'If r.HighlightColorIndex = wdDarkYellow Then 'highlightcols(7)
                       If r.Bold Then
                          ThatDoc.Range.Characters.Last.FormattedText = .FormattedText
                          ThatDoc.Range.InsertParagraphAfter
                          .Collapse 0
                       End If
                     Loop
                     
                End With
                
                'add filename to list only if bold has been found
                If fWritten = True Then
                    FileNames = FileNames & vbCrLf & ThisDoc.Name
                    fWritten = False
                End If
            End If
        Next
    
    'Add list of filenames to the end of ThatDoc
    With ThatDoc.Content
        .InsertParagraphAfter
        .InsertAfter FileNames
    End With
        
    cleanUp:
    Application.ScreenUpdating = True
    Set ThatDoc = Nothing
    Set ThisDoc = Nothing
    End Sub
    
    

    【讨论】:

    • 您好,这真的很好,但是,可以颠倒顺序吗?即按原始文件打开的顺序。
    • 为此,文档集合必须“从后面”阅读……那么您不能使用for each,而是按索引进行迭代——但顺序相反
    • 嗨,我想可能是这样的。那超出了我的范围。我应该提出一张新票并关闭这张票吗?
    • 对不起 - 我忘了写我相应地调整了答案中的代码
    • 太棒了! lke,最后一件事......我想将那些包含粗体文本的打开文档的文档名称发送到创建的 ThatDoc 的末尾(在结果之后)。这可能吗?
    猜你喜欢
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    相关资源
    最近更新 更多