Issue with Document Splitting VBA Code – Incorrect Number of Documents Extracted

I’m encountering an issue with a VBA code that I’m using in MS Word to split a document into separate files based on a specific criteria. The document I’m working with, “WHITGL0T1.docx,” has 47 pages. The goal is to extract separate documents whenever the phrase “Ntra Ref” is found.

I have tried using the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Sub SplitDocument()
Dim SourcePath As String
Dim DestinationPath As String
Dim SourceDoc As Document
Dim i As Integer
Dim isDocumentStart As Boolean
Dim docNumber As Integer
Dim startPage As Integer
Dim endPage As Integer
SourcePath = "C:Usersu1285829OneDrive - MMCDesktopSpainEurosys PDF"
DestinationPath = "C:Usersu1285829OneDrive - MMCDesktopSpainSplitted documents"
docNumber = 1
isDocumentStart = False
Set SourceDoc = Documents.Open(SourcePath & "WHITGL0T1.docx")
For i = 1 To SourceDoc.ComputeStatistics(wdStatisticPages)
If SourceDoc.Range.GoTo(wdGoToPage, wdGoToAbsolute, i).Find.Execute(FindText:="Ntra Ref", MatchWholeWord:=True) Then
If Not isDocumentStart Then
startPage = i
isDocumentStart = True
Else
endPage = i - 1
SourceDoc.ExportAsFixedFormat2 OutputFileName:=DestinationPath & "Document_" & docNumber & ".pdf", ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=startPage, To:=endPage
docNumber = docNumber + 1
startPage = i
End If
End If
Next i
If isDocumentStart Then
endPage = SourceDoc.ComputeStatistics(wdStatisticPages)
SourceDoc.ExportAsFixedFormat2 OutputFileName:=DestinationPath & "Document_" & docNumber & ".pdf", ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=startPage, To:=endPage
End If
SourceDoc.Close SaveChanges:=False
MsgBox "The document has been split into separate documents based on the 'Ntra Ref' criteria and saved in the destination folder.", vbInformation
End Sub
</code>
<code>Sub SplitDocument() Dim SourcePath As String Dim DestinationPath As String Dim SourceDoc As Document Dim i As Integer Dim isDocumentStart As Boolean Dim docNumber As Integer Dim startPage As Integer Dim endPage As Integer SourcePath = "C:Usersu1285829OneDrive - MMCDesktopSpainEurosys PDF" DestinationPath = "C:Usersu1285829OneDrive - MMCDesktopSpainSplitted documents" docNumber = 1 isDocumentStart = False Set SourceDoc = Documents.Open(SourcePath & "WHITGL0T1.docx") For i = 1 To SourceDoc.ComputeStatistics(wdStatisticPages) If SourceDoc.Range.GoTo(wdGoToPage, wdGoToAbsolute, i).Find.Execute(FindText:="Ntra Ref", MatchWholeWord:=True) Then If Not isDocumentStart Then startPage = i isDocumentStart = True Else endPage = i - 1 SourceDoc.ExportAsFixedFormat2 OutputFileName:=DestinationPath & "Document_" & docNumber & ".pdf", ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=startPage, To:=endPage docNumber = docNumber + 1 startPage = i End If End If Next i If isDocumentStart Then endPage = SourceDoc.ComputeStatistics(wdStatisticPages) SourceDoc.ExportAsFixedFormat2 OutputFileName:=DestinationPath & "Document_" & docNumber & ".pdf", ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=startPage, To:=endPage End If SourceDoc.Close SaveChanges:=False MsgBox "The document has been split into separate documents based on the 'Ntra Ref' criteria and saved in the destination folder.", vbInformation End Sub </code>
Sub SplitDocument()
    Dim SourcePath As String
    Dim DestinationPath As String
    Dim SourceDoc As Document
    Dim i As Integer
    Dim isDocumentStart As Boolean
    Dim docNumber As Integer
    Dim startPage As Integer
    Dim endPage As Integer

    SourcePath = "C:Usersu1285829OneDrive - MMCDesktopSpainEurosys PDF"
    DestinationPath = "C:Usersu1285829OneDrive - MMCDesktopSpainSplitted documents"
    docNumber = 1
    isDocumentStart = False

    Set SourceDoc = Documents.Open(SourcePath & "WHITGL0T1.docx")

    For i = 1 To SourceDoc.ComputeStatistics(wdStatisticPages)
        If SourceDoc.Range.GoTo(wdGoToPage, wdGoToAbsolute, i).Find.Execute(FindText:="Ntra Ref",            MatchWholeWord:=True) Then
            If Not isDocumentStart Then
                startPage = i
                isDocumentStart = True
            Else
                endPage = i - 1
                SourceDoc.ExportAsFixedFormat2 OutputFileName:=DestinationPath & "Document_" & docNumber &     ".pdf", ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=startPage, To:=endPage
                docNumber = docNumber + 1
                startPage = i
            End If
        End If
    Next i

    If isDocumentStart Then
        endPage = SourceDoc.ComputeStatistics(wdStatisticPages)
        SourceDoc.ExportAsFixedFormat2 OutputFileName:=DestinationPath & "Document_" & docNumber & ".pdf",      ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=startPage, To:=endPage
    End If

    SourceDoc.Close SaveChanges:=False

    MsgBox "The document has been split into separate documents based on the 'Ntra Ref' criteria and saved   in the destination folder.", vbInformation
End Sub

The code seems to be working, but it is extracting all 47 pages as separate documents, even though the phrase “Ntra Ref” appears only 44 times in the document. I expected to have 44 separate documents extracted.

I have reviewed the code and made sure that the logic is correct. However, I’m unable to identify the issue causing this behavior. I suspect that there might be a problem with the condition or loop that determines when to start and end a document extraction.

Could someone please review the code and provide any insights or suggestions on how to fix this issue? I would greatly appreciate any help or guidance.

Thank you in advance!

1

(Problems outlined below the code). Instead, try starting with:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Sub SplitDocument()
Dim SourcePath As String
Dim DestinationPath As String
Dim SourceDoc As Document
Dim i As Integer
Dim isDocumentStart As Boolean
Dim docNumber As Integer
Dim startPage As Integer
Dim endPage As Integer
Dim r As Range
SourcePath = "C:Usersu1285829OneDrive - MMCDesktopSpainEurosys PDF"
DestinationPath = "C:Usersu1285829OneDrive - MMCDesktopSpainSplitted documents"
docNumber = 1
isDocumentStart = False
Set SourceDoc = Documents.Open(SourcePath & "WHITGL0T1.docx")
Set r = SourceDoc.Range
docNumber = 0
Do While r.Find.Execute(FindText:="Ntra Ref", MatchWholeWord:=True)
docNumber = docNumber + 1
SourceDoc.ExportAsFixedFormat2 _
OutputFileName:=DestinationPath & "Document_" & docNumber & ".pdf", _
ExportFormat:=wdExportFormatPDF, _
Range:=wdExportFromTo, _
From:=r.Information(wdActiveEndPageNumber), _
To:=r.Information(wdActiveEndPageNumber)
Loop
SourceDoc.Close SaveChanges:=False
MsgBox "The document has been split into separate documents based on the 'Ntra Ref' criteria and saved in the destination folder.", vbInformation
End Sub
</code>
<code>Sub SplitDocument() Dim SourcePath As String Dim DestinationPath As String Dim SourceDoc As Document Dim i As Integer Dim isDocumentStart As Boolean Dim docNumber As Integer Dim startPage As Integer Dim endPage As Integer Dim r As Range SourcePath = "C:Usersu1285829OneDrive - MMCDesktopSpainEurosys PDF" DestinationPath = "C:Usersu1285829OneDrive - MMCDesktopSpainSplitted documents" docNumber = 1 isDocumentStart = False Set SourceDoc = Documents.Open(SourcePath & "WHITGL0T1.docx") Set r = SourceDoc.Range docNumber = 0 Do While r.Find.Execute(FindText:="Ntra Ref", MatchWholeWord:=True) docNumber = docNumber + 1 SourceDoc.ExportAsFixedFormat2 _ OutputFileName:=DestinationPath & "Document_" & docNumber & ".pdf", _ ExportFormat:=wdExportFormatPDF, _ Range:=wdExportFromTo, _ From:=r.Information(wdActiveEndPageNumber), _ To:=r.Information(wdActiveEndPageNumber) Loop SourceDoc.Close SaveChanges:=False MsgBox "The document has been split into separate documents based on the 'Ntra Ref' criteria and saved in the destination folder.", vbInformation End Sub </code>
Sub SplitDocument()
    Dim SourcePath As String
    Dim DestinationPath As String
    Dim SourceDoc As Document
    Dim i As Integer
    Dim isDocumentStart As Boolean
    Dim docNumber As Integer
    Dim startPage As Integer
    Dim endPage As Integer
    Dim r As Range

    SourcePath = "C:Usersu1285829OneDrive - MMCDesktopSpainEurosys PDF"
    DestinationPath = "C:Usersu1285829OneDrive - MMCDesktopSpainSplitted documents"
    docNumber = 1
    isDocumentStart = False

    Set SourceDoc = Documents.Open(SourcePath & "WHITGL0T1.docx")
    Set r = SourceDoc.Range
    docNumber = 0
    Do While r.Find.Execute(FindText:="Ntra Ref", MatchWholeWord:=True)
      docNumber = docNumber + 1
      SourceDoc.ExportAsFixedFormat2 _
        OutputFileName:=DestinationPath & "Document_" & docNumber &     ".pdf", _
        ExportFormat:=wdExportFormatPDF, _
        Range:=wdExportFromTo, _
        From:=r.Information(wdActiveEndPageNumber), _
        To:=r.Information(wdActiveEndPageNumber)
    Loop

    SourceDoc.Close SaveChanges:=False

    MsgBox "The document has been split into separate documents based on the 'Ntra Ref' criteria and saved   in the destination folder.", vbInformation
End Sub

(Or if that is picking up the wrong pages, you could probably resort to using Selection.Find and wdExportCurrentPage in which case you do not need to retrieve the page numbers).

I think you are assuming that Range.Find will only find text in the currently specified Range, whereas in fact if (say) the first occurence of "Ntra Ref" is on page 2, the first .Execute will succeed, but the startPage is still set to 1, the code will output a PDF for page 1, and so on.

NB,if an “Ntra Ref” spans an automatic page break, this code will emit the second of the two pages.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật