troubleshooting Question

MS Access Using VBA Split Word Docx file into separate Doc for Each Page

Avatar of Rick Rudolph
Rick RudolphFlag for United States of America asked on
Microsoft OfficeMicrosoft ApplicationsVBA
15 Comments1 Solution891 ViewsLast Modified:
In Microsoft Access, I want to "read" a Microsoft Word document that was created through a Mail Merge and split it into as many documents as there are pages. I would like to name each document based upon the characters found on each page between two ^'s. Below is some code that was on the .net Experts Exchange forum for doing the split within Word (without the "special" naming). So my first question is this:

In Access, how would I implement the code below?

When I cut and paste the code, the First Reference to WordApp, is colored Red, which leads me to believe I need to do something with the references.

Public Sub ParseDoc(ByVal filename As String)

    Dim WordApp As New Microsoft.Office.Interop.Word.Application
    Dim docMultiple As Microsoft.Office.Interop.Word.Document
    Dim docSingle As Microsoft.Office.Interop.Word.Document
    Dim rngPage As Microsoft.Office.Interop.Word.Range
    Dim iCurrentPage As Integer
    Dim iPageCount As Integer
    Dim strNewFileName As String

    WordApp.Application.ScreenUpdating = False
    docMultiple = WordApp.Documents.Open(filename)

    rngPage = docMultiple.Range
    iCurrentPage = 1
    iPageCount = docMultiple.Content.ComputeStatistics(WdStatistic.wdStatisticPages)

    Do Until iCurrentPage > iPageCount
        If iCurrentPage = iPageCount Or iCurrentPage = iPageCount - 1 Then
            rngPage.End = WordApp.ActiveDocument.Range.End
            WordApp.Selection.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, iCurrentPage + 2)
            rngPage.End = WordApp.Selection.Start
        End If
        docSingle = WordApp.Documents.Add
        docSingle.range.Find.Execute(Findtext:="^m", ReplaceWith:="")
        strNewFileName = Replace(docMultiple.FullName, ".docx", "_" & Right$("000" & iCurrentPage, 4) & ".docx")
        docSingle.SaveAs (strNewFileName)
        iCurrentPage = iCurrentPage + 1
        rngPage.Collapse (WdCollapseDirection.wdCollapseEnd)

    WordApp.Application.ScreenUpdating = True


    docMultiple = Nothing
    docSingle = Nothing
    rngPage = Nothing
    WordApp = Nothing

End Sub

My next question relates to the naming of the files. Below is a snippet of code I had that was renaming some similar pdf documents. How would I search the page contents looking for the string so I can replace using the page number as the file name? So I am really asking how do I load the entire page into the FileContent string variable.

FirstCaret = InStr(1, FileContent, "^")
NextCaret = InStr((FirstCaret + 1), FileContent, "^")
NewName = Mid(FileContent, (FirstCaret + 1), (NextCaret - (FirstCaret + 1)))
Join our community to see this answer!
Unlock 1 Answer and 15 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros