View Single Post
 
Old 02-20-2023, 05:17 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

My code was writing the list to a string variable called str. You need to place the contents of this string into the new document.

Try this version which creates the new doc and puts the contents into it.
Code:
Sub List()
  Dim oDoc As Document, oTarget As Document
  Dim oRng As Range, oPara As Range
  Dim str As String, arr() As String, i As Integer
  
  Set oDoc = ActiveDocument
  Set oRng = oDoc.Range
  With oRng.Find
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = False
    .Text = "#"
    Do While .Execute = True
      Set oPara = oRng.Paragraphs(1).Range
      Debug.Print oPara.Text
      arr = Split(oPara.Text, "#")
      For i = 1 To UBound(arr)
        str = str & arr(0) & vbCr
      Next i
      oRng.Start = oPara.End
    Loop
  End With
  If str <> "" Then
    Set oTarget = Documents.Add
    oTarget.Range.Text = str    'put list into new unsaved doc
  End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote