wpfoffice-interop

Returning x pages from a Word document and creating a new document with them


Here is the plan - it's a good plan, but there is the proverbial brick wall. I have Word documents stored as Byte() on a database - I need to pull selected ones and save to a temp directory as word document. That is done and working. Next, I need to create a new document based on the first x pages of the original, save that to the temp directory - email the new file and delete the contents of the temp directory.

The only part I am stuck on is splitting the file by page number. This, as an example, returns the number of pages...

Public Function ReturnWordPages(FileName As String) As Integer
Try
    Dim WordApp As ApplicationClass = New ApplicationClass()
    Dim vFilename As Object = FileName
    Dim [readonly] As Object = False
    Dim isVisible As Object = True
    Dim oMissing As Object = System.Reflection.Missing.Value
    Dim wDoc As Document = WordApp.Documents.Open(vFilename, oMissing, [readonly], oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, isVisible)
    Dim vStat As WdStatistic = WdStatistic.wdStatisticPages
    Dim vNum As Integer = wDoc.ComputeStatistics(vStat, oMissing)
    Return vNum

Catch ex As Exception
    EmailError(ex)
    Return 0
End Try
End Function

Any pointers would be appreciated.

======== UPDATE ===============

I have got a little further - able to copy the selected number of pages and save the new file, but in the process Word throws a popup - originalFile.docx is locked for editing by localComputerName - Do you want to open a read only copy, create as local copy....

How do I manage to get the entire process to run without prompts and opening and closing of the file? Also, it's not copying over page numbers (footers).

I have tried setting visible to false - that has no effect

 Public Function ReturnSplitDocument(FileName As String, vPages As Integer, outputPath As String) As Boolean
 Try
     Dim WordApp As ApplicationClass = New ApplicationClass()
     Dim BaseDoc As Document
     Dim DestDoc As Document
     Dim vWhat As Object = WdGoToItem.wdGoToPage
     Dim vWhich As Object = WdGoToDirection.wdGoToFirst
     Dim vCount As Object = 1
     Dim vFilename As Object = FileName
     Dim [readonly] As Object = False
     Dim isVisible As Object = True
     Dim oMissing As Object = System.Reflection.Missing.Value
     BaseDoc = WordApp.Documents.Open(FileName)
     WordApp.Documents.Open(vFilename, oMissing, [readonly], oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, isVisible)


     Dim vStartRange As Range = WordApp.Selection.GoTo(vWhat, vWhich, vCount, oMissing)
     Dim vCount2 As Object = CInt(vCount) + vPages
     Dim vEndRange As Range = WordApp.Selection.GoTo(vWhat, vWhich, vCount2, oMissing)
     vEndRange.SetRange(vStartRange.Start, vEndRange.End - 1)
     vEndRange.Select()
     WordApp.Selection.Copy()
     DestDoc = WordApp.Documents.Add
     DestDoc.Activate()
     WordApp.Selection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting)
     DestDoc.SaveAs2(outputPath)
     DestDoc.Close()
     DestDoc = Nothing

     WordApp.Quit()
     WordApp = Nothing

     Return True

 Catch ex As Exception
     EmailError(ex)
     Return False
 End Try
 End Function

Solution

  • The solution is to add another .visible = false whilst the application has the destination file open and before .activate.

    Public Function ReturnSplitDocument(FileName As String, vPages As Integer, outputPath As String) As Boolean
    Try
        Dim WordApp As ApplicationClass = New ApplicationClass()
        WordApp.Visible = False
        Dim BaseDoc As Document
        Dim DestDoc As Document
        Dim vWhat As Object = WdGoToItem.wdGoToPage
        Dim vWhich As Object = WdGoToDirection.wdGoToFirst
        Dim vCount As Object = 1
        Dim vFilename As Object = FileName
        Dim [readonly] As Object = True
        Dim isVisible As Object = False
        Dim oMissing As Object = System.Reflection.Missing.Value
        BaseDoc = WordApp.Documents.Open(vFilename)
        WordApp.Documents.Open(vFilename, oMissing, [readonly], oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, isVisible, oMissing, oMissing, oMissing, oMissing)
    
    
        Dim vStartRange As Range = WordApp.Selection.GoTo(vWhat, vWhich, vCount, oMissing)
        Dim vCount2 As Object = CInt(vCount) + vPages
        Dim vEndRange As Range = WordApp.Selection.GoTo(vWhat, vWhich, vCount2, oMissing)
        vEndRange.SetRange(vStartRange.Start, vEndRange.End - 1)
        vEndRange.Select()
        WordApp.Selection.Copy()
        DestDoc = WordApp.Documents.Add
        WordApp.Visible = False  '<------------- Solution
        DestDoc.Activate()
        WordApp.Selection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting)
        DestDoc.SaveAs2(outputPath)
        DestDoc.Close()
        DestDoc = Nothing
        BaseDoc.Close()
        BaseDoc = Nothing
    
        WordApp.Quit()
        WordApp = Nothing
    
        Return True
    
    Catch ex As Exception
        EmailError(ex)
        Return False
    End Try
    End Function