0
votes

I have multiple large docx files (word 2010) which need to be split on basis of a delimiter ("///"). I tried using a macro given http://www.vbaexpress.com/forum/showthread.php?39733-Word-File-splitting-Macro-question

However it gives an error "This method or Property is not available since No Text is Selected" on the line colNotes(i).Copy (Sub SplitNotes(...)).

The macro is reproduced below:

Sub testFileSplit()
    Call SplitNotes("///", "C:\Users\myPath\temp_DEL_008_000.docx")
End Sub
Sub SplitNotes(strDelim As String, strFilename As String)
    Dim docNew As Document
    Dim i As Long
    Dim colNotes As Collection
    Dim temp As Range

    'get the collection of ranges
    Set colNotes = fGetCollectionOfRanges(ActiveDocument, strDelim)

    'see if the user wants to proceed
    If MsgBox("This will split the document into " & _
    colNotes.Count & _
    " sections. Do you wish to proceed?", vbYesNo) = vbNo Then
        Exit Sub
    End If

     'go through the collection of ranges
    For i = 1 To colNotes.Count
         'create a new document
        Set docNew = Documents.Add

        'copy our range
        colNotes(i).Copy
         'paste it in
        docNew.Content.Paste
         'save it
        docNew.SaveAs fileName:=ThisDocument.path & "\" & strFilename & Format(i, "000"), FileFormat:=wdFormatDocument

        docNew.Close
    Next
End Sub
Function fGetCollectionOfRanges(oDoc As Document, strDelim As String) As Collection
    Dim colReturn As Collection
    Dim rngSearch As Range
    Dim rngFound As Range

     'initialize a new collection
    Set colReturn = New Collection
     'initialize our starting ranges
    Set rngSearch = oDoc.Content
    Set rngFound = rngSearch.Duplicate

     'start our loop
    Do
         'search through
        With rngSearch.Find
            .Text = strDelim
            .Execute
             'if we found it... prepare to add to our collection
            If .Found Then
                 'redefine our rngfound
                rngFound.End = rngSearch.Start
                 'add it to our collection
                colReturn.Add rngFound.Duplicate
                 'reset our search and found for the next
                rngSearch.Collapse wdCollapseEnd
                rngFound.Start = rngSearch.Start
                rngSearch.End = oDoc.Content.End
            Else
                 'if we didn't find, exit our loop
                Exit Do
            End If
        End With
         'shouldn't ever hit this... unless the delimter passed in is a VBCR
    Loop Until rngSearch.Start >= ActiveDocument.Content.End

     'and return our collection
    Set fGetCollectionOfRanges = colReturn
End Function
1
I think the error message stays exactly what the problem is. Try to add colNotes(i).Range.Select just before the line which throws the error.Kazimierz Jawor

1 Answers

0
votes

For those who might be interested: The code does work in 2010. The issue was a delimiter which was the first thing on the file... Deleted it and it worked...