Resolved Print Word Document Range Issue

ggunter

Well-known member
Joined
Apr 23, 2008
Messages
137
Programming Experience
Beginner
Code:
VB.NET:
Public Function PrintDocument(ByVal pageFrom As Object, ByVal pageTo As Object, _
            Optional ByVal displayErrorMessage As Boolean = True) As PrintReturns

        Try
            wrdApp.ActiveDocument.PrintOut(, , WdPrintOutRange.wdPrintFromTo, , pageFrom, pageTo, WdPrintOutItem.wdPrintDocumentContent, , , , , , , , , , , )

            PrintDocument = PrintReturns.Ok
        Catch ex As Exception
            If displayErrorMessage Then
                MessageBox.Show(ex.Message)
            End If

            PrintDocument = PrintReturns.pError
        End Try

    End Function

Issue:
Whenever I try to print a range, I receive the following error message:
Type Mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

I have checked Help and it shows my types to be correct. My google results just say a type is wrong but don't give anything more specific. If I use the "PrintOut" command with no parameters, it works fine.

Any help would be greatly appreciated.
 
Last edited:
Isn't that always the way: search for a full day, post the question, find the answer 5 mins. later.

The PageFrom and PageTo parameters are listed in Help as "Objects". One would naturally assume that these would be numbers, as in page 'number'. But they are, in fact, strings.

Here's the modified code:
VB.NET:
Public Function PrintDocument(ByVal pageFrom As Object, ByVal pageTo As Object, _
            Optional ByVal displayErrorMessage As Boolean = True) As PrintReturns
        Dim myFrom As Object = pageFrom.ToString()
        Dim myTo As Object = pageTo.ToString()

        Try
            wrdApp.ActiveDocument.PrintOut(, , WdPrintOutRange.wdPrintFromTo, , myFrom, myTo, WdPrintOutItem.wdPrintDocumentContent, , , , , , , , , , , )

            PrintDocument = PrintReturns.Ok
        Catch ex As Exception
            If displayErrorMessage Then
                MessageBox.Show(ex.Message)
            End If

            PrintDocument = PrintReturns.pError
        End Try

    End Function
 
Back
Top