Hi,

I'm writing a function that takes a word doc and returns the number of pages in it. I have the function working and returning pages except that it opens a Word dialog asking if I want to save changes - does anyone know how to stop this? I have the document running "silently" in the background so I don't want to spoil the feel by prompting for saves.
Here is my code:

Private Function CountWordPages(ByRef Doc As String) As Integer
        Dim oWord As Object
        Dim WD_Doc As Word.Document
        Dim intCount As Integer = 0
        On Error GoTo ErrorHandler
        oWord = CreateObject("Word.Application") 'late bind for differing versions of word
        WD_Doc = oWord.Documents.Open(Doc, True, True, False, , , , , , , , False) 'open silently in background
        intCount = WD_Doc.ComputeStatistics(Word.WdStatistic.wdStatisticPages) 'get count
        WD_Doc = Nothing 'done with the doc
        oWord.Application.Quit() ' close the app
        oWord = Nothing 'tidy up
        CountWordPages = intCount 'return count
        Exit Function
ErrorHandler:
        DisplayError(Err.Number, Err.Description)
    End Function

Thanks in advance

Dani AI

Generated

Nice catch by on using DoNotSave when quitting. One more tweak that keeps Word fully silent is to explicitly close the document first, suppress alerts, and then quit the application. Also, release COM references or you may leave a hidden WINWORD.EXE running. A simple Try/Finally pattern does all of this cleanly.

Private Function GetWordPageCount(path As String) As Integer
    Dim app As Word.Application = Nothing
    Dim doc As Word.Document = Nothing
    Try
        app = New Word.Application()
        app.Visible = False
        app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone

        doc = app.Documents.Open(FileName:=path,
                                 ReadOnly:=True,
                                 AddToRecentFiles:=False,
                                 Visible:=False)

        Dim pages As Integer =
            CInt(doc.ComputeStatistics(Word.WdStatistic.wdStatisticPages))
        Return pages

    Finally
        If doc IsNot Nothing Then
            doc.Close(SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges)
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc)
        End If
        If app IsNot Nothing Then
            app.DisplayAlerts = Word.WdAlertLevel.wdAlertsAll
            app.Quit(SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges)
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app)
        End If
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try
End Function

Notes:

  • If you prefer late binding like , use the numeric constant 0 for DoNotSave when calling Close and Quit, and avoid referencing Word enums directly.
  • Use AddToRecentFiles:=False to keep the doc off the MRU list.
  • In ASP.NET, running Office automation on the server is fragile and not supported; consider a service or library that can read pagination without launching Word. , this is a common gotcha when moving code from desktop to web.

Hi All,

I managed to figure it out, I should have closed with:

oWord.Application.Quit(False)

Thanks for the info on how you fixed it. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.