none
Using For Each...Next block structures in Word RRS feed

  • Question

  • Hello,

    I would like to know more in regards to using the For Each...Next structure block.

    For example, if user detects bold font, then store it in an array string.

    How would I perform that, even though I have the logic correctly?

    Regards,

    JohnDBCTX


    jp

    Tuesday, March 27, 2018 8:06 AM

Answers

  • E.g.

    Option Explicit
    
    Public Sub Test()
      
      Dim Bolds As VBA.Collection
      Dim Word As Word.Range
      
      Dim Bold As Variant
      
      Set Bolds = New VBA.Collection
      For Each Word In Selection.Words
        If Word.Font.Bold Then
          Bolds.Add Word.Text
        End If
      Next Word
    
      Set Word = Nothing
      
      For Each Bold In Bolds
        Debug.Print Bold
      Next Bold
      
      Set Bold = Nothing
      Set Bolds = Nothing
      
    End Sub
    

    • Marked as answer by JohnDBCTX Tuesday, March 27, 2018 9:10 AM
    Tuesday, March 27, 2018 8:33 AM
  • I have added some lines and comments to your code snippet just to clarify.

    This time, I have output this in Notepad at runtime successfully.

    Option Explicit
    Dim strX As String
    
    Public Sub Test()
    Open "C:\Users\johnd\Documents\Output This One.txt" For Output As #1
    
      Dim Bolds As VBA.Collection
      Dim Word As Word.Range
      
      Dim Bold As Variant
      
      Set Bolds = New VBA.Collection
      For Each Word In Selection.Words
        If Word.Font.Bold Then
          Bolds.Add Word.Text
          strX = Word.Text   'Put bold text in parameter to reserve in memory at runtime.
          Print #1, strX     'Output this in Notepad.
        End If
      Next Word
    
      Set Word = Nothing
      
      For Each Bold In Bolds
        Debug.Print Bold
      Next Bold
      
      Set Bold = Nothing
      Set Bolds = Nothing
      Close
      
    End Sub

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Tuesday, March 27, 2018 9:10 AM
    Tuesday, March 27, 2018 9:10 AM

All replies

  • E.g.

    Option Explicit
    
    Public Sub Test()
      
      Dim Bolds As VBA.Collection
      Dim Word As Word.Range
      
      Dim Bold As Variant
      
      Set Bolds = New VBA.Collection
      For Each Word In Selection.Words
        If Word.Font.Bold Then
          Bolds.Add Word.Text
        End If
      Next Word
    
      Set Word = Nothing
      
      For Each Bold In Bolds
        Debug.Print Bold
      Next Bold
      
      Set Bold = Nothing
      Set Bolds = Nothing
      
    End Sub
    

    • Marked as answer by JohnDBCTX Tuesday, March 27, 2018 9:10 AM
    Tuesday, March 27, 2018 8:33 AM
  • I have added some lines and comments to your code snippet just to clarify.

    This time, I have output this in Notepad at runtime successfully.

    Option Explicit
    Dim strX As String
    
    Public Sub Test()
    Open "C:\Users\johnd\Documents\Output This One.txt" For Output As #1
    
      Dim Bolds As VBA.Collection
      Dim Word As Word.Range
      
      Dim Bold As Variant
      
      Set Bolds = New VBA.Collection
      For Each Word In Selection.Words
        If Word.Font.Bold Then
          Bolds.Add Word.Text
          strX = Word.Text   'Put bold text in parameter to reserve in memory at runtime.
          Print #1, strX     'Output this in Notepad.
        End If
      Next Word
    
      Set Word = Nothing
      
      For Each Bold In Bolds
        Debug.Print Bold
      Next Bold
      
      Set Bold = Nothing
      Set Bolds = Nothing
      Close
      
    End Sub

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Tuesday, March 27, 2018 9:10 AM
    Tuesday, March 27, 2018 9:10 AM