How to use Document.PrintOut method from Microsoft.Office.Tools.Word to print the first page of a document

0

The documentation is here: https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.word.document.printout?view=vsto-2017

This will print the whole document:

Word.Application ap = new Word.Application();
Word.Document document = ap.Documents.Open(@"C:\temp\file.doc");
document.PrintOut();

I thought I were on to something with this as it compiled but it didn't work:

Word.Application ap = new Word.Application();
Word.Document document = ap.Documents.Open(@"C:\temp\file.doc");
Word.WdPrintOutRange printRange = new Word.WdPrintOutRange();
document.PrintOut(false, false, printRange,false, 1, 2);

System.Runtime.InteropServices.COMException: 'Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))'

How do I use this method to print just the first page of the document?

Edit: This URL (https://docs.microsoft.com/en-us/office/vba/api/word.document.printout) shows examples in VBA on how to do similar things, such as print the first 3 pages, but it's in VB and I am not sure on the c# equivalent.

c#
asked on Stack Overflow May 11, 2021 by user2924019

2 Answers

1

Word.WdPrintOutRange is an enum value. And wdPrintFromTo is for range selection.

Word.WdPrintOutRange printRange = Word.WdPrintOutRange.wdPrintFromTo;
document.PrintOut(false, false, printRange, null, 1, 2);
answered on Stack Overflow May 11, 2021 by Eldar
0

I couldn't get range to work.

I managed to achieve printing just the first page with this instead (Added some margins and setup the page size too):

Word.Application ap = new Word.Application();
Word.Document document = ap.Documents.Open(randFile);

document.PageSetup.TopMargin = 50;
document.PageSetup.RightMargin = 50;
document.PageSetup.BottomMargin = 50;
document.PageSetup.LeftMargin = 50;
document.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4;

Word.WdPrintOutRange printRange = Word.WdPrintOutRange.wdPrintCurrentPage;

document.PrintOut(false,null,printRange);
document.Close(false, false, false);
answered on Stack Overflow May 11, 2021 by user2924019

User contributions licensed under CC BY-SA 3.0