Share via

Split and name Word file using VBA script

Anonymous
2017-09-11T16:09:11+00:00

I have a file that I would like to split into multiple files using a specific delimiter. I would like to have the resulting files named using the text that immediately follows the delimiter. I have found several examples of people trying to do this, but none of the script examples work for me.

Here is a link with a small file showing what I am trying to do: https://digitalmeasures-my.sharepoint.com/personal/dsarnowski_digitalmeasures_com/_layouts/15/guestaccess.aspx?docid=1646acdaa4b2c45859090b24f90f17d4e&authkey=AZ4tRnX30dQRw9-8enYBCTk

Here is the content of the file:

///DavidSarnowski

David Sarnowski

Report Content

Report Footer

///LeahWilliams

Leah Williams

Report Content

Report Footer

///BrettSmith

BrettSmith

Report Content

Report Footer

My desired output would look like this:

Three files:

  • DavidSarnowski.doc
  • LeahWilliams.doc
  • BrettSmith.doc

The content of the file for DavidSarnowski.doc would look like this:

David Sarnowski

Report Content

Report Footer

I am a VBA newbie and not sure if this can work and/or how you would do it. Any help would be greatly appreciated.

***Post moved by the moderator to the appropriate forum category.***

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2017-09-13T21:39:04+00:00

    Try downloading from: OneDrive

    Note: If you copied & pasted the code directly from my post above into the VBE, it's possible you ended up with a bunch of invalid non-breaking spaces in the code. If you paste the code into an empty Word document and do a Find/Replace, with:

    Find = ^s

    Replace = ^32

    That will turn them back into normal spaces. You should then be able to paste the code into the VBE and have it work correctly.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-09-13T15:20:12+00:00

    Paul, can you link to or attach a copy of a Word document with the macro in it so I could run it on my end?

    Was this answer helpful?

    0 comments No comments
  3. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2017-09-12T20:50:21+00:00

    The code works fine for me with the sample data in your linked document. Did any new documents get created?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-09-12T13:37:46+00:00

    @Macropod, thank for giving this a shot. When I ran the macro I received this error:

    "Run-time error '438':

    Object doesn't support this property or method"

    When I debugged it, it highlighted this line:

    .SaveAs2 fileName:=FlPth & StrNm & Ext, Fileformat:=Fmt, AddToRecentFiles:=False

    Any thoughts?

    Was this answer helpful?

    0 comments No comments
  5. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2017-09-11T22:58:20+00:00

    Try:

    Sub SplitDoc()

    Application.ScreenUpdating = False

    Dim Rng As Range, DocSrc As Document, DocTgt As Document, StrNm As String

    Dim Ext As String, FlPth As String, Tmplt As String, Fmt As Long

    Set DocSrc = ActiveDocument

    With DocSrc

      Ext = "." & Split(.Name, ".")(UBound(Split(.Name, ".")))

      FlPth = .Path & ""

      Tmplt = .AttachedTemplate.FullName

      Fmt = .SaveFormat

      With .Range

        .InsertAfter vbCr & "///"

        With .Find

          .ClearFormatting

          .Replacement.ClearFormatting

          .Text = "[//]{3}*^13"

          .Replacement.Text = ""

          .Forward = True

          .Wrap = wdFindStop

          .Format = False

          .MatchWildcards = True

          .Execute

        End With

        Do While .Find.Found

          If .End = DocSrc.Range.End Then GoTo Finished

          Set Rng = .Duplicate

          With Rng

            .MoveEndUntil "///", wdForward

            StrNm = Split(Split(.Paragraphs.First.Range.Text, vbCr)(0), "///")(1)

            .Start = .Paragraphs.First.Range.End

            Set DocTgt = Documents.Add(Tmplt)

            With DocTgt

              .Range.FormattedText = Rng.FormattedText

              .SaveAs2 FileName:=FlPth & StrNm & Ext, Fileformat:=Fmt, AddToRecentFiles:=False

              .Close False

            End With

          End With

          .Collapse wdCollapseEnd

          .Find.Execute

        Loop

      End With

    End With

    Finished:

    Undo: Set Rng = Nothing: Set DocTgt = Nothing: Set DocSrc = Nothing

    Application.ScreenUpdating = True

    End Sub

    Was this answer helpful?

    0 comments No comments