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
Else
WordApp.Selection.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, iCurrentPage + 2)
rngPage.End = WordApp.Selection.Start
End If
rngPage.Copy()
docSingle = WordApp.Documents.Add
docSingle.range.Paste()
docSingle.range.Find.Execute(Findtext:="^m", ReplaceWith:="")
strNewFileName = Replace(docMultiple.FullName, ".docx", "_" & Right$("000" & iCurrentPage, 4) & ".docx")
docSingle.SaveAs (strNewFileName)
iCurrentPage = iCurrentPage + 1
docSingle.Close()
rngPage.Collapse (WdCollapseDirection.wdCollapseEnd)
Loop
WordApp.Application.ScreenUpdating = True
WordApp.Quit()
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)))
pls try
Open in new window
single Page contentstrText = docSingle.Range.Text
Regards