I'm trying to print a range of pages from my Ole Object object, but it's not working for me - I get a "type mismatch" exception when the call to Doc.PrintOut executes.
Can anyone help me to avoid this error? Below is the code used in tests:
if (OpenDialog1.Execute) then
begin
// Cria objeto principal de controle do Word
WinWord := CreateOleObject('Word.Application');
if (not (VarIsEmpty(WinWord))) then
begin
// Mostra o Word
try
WinWord.Visible := false;
Docs := WinWord.Documents;
// Abre um documento
Doc := Docs.Open(OpenDialog1.FileName);
//Doc.PrintOut(false, Range, 1, 2);
//Doc.PrintOut(Copies := 2);
vFrom := 1;
vTo := 2;
Doc.PrintOut(Background := false, Append := false, Range := wdPrintFromTo, OutputFileName := EmptyParam, From := vFrom, To := vTo);
// erro recebido: tipo não correspondente
finally
// Fecha o Word
WinWord.Quit;
end;
end;
showmessage('Fim!');
end;
I tried your code in D7, using the English-language version of Office2007.
I get an exception error on Doc.PrintOut which is Delphi's usual "Type Mismatch". By experimenting with the arguments passed to Doc.PrintOut, I think it's the
Range := wdPrintFromTo
that's causing the exception, because a "range" would normally be a block of text, whereas wdPrintFromTo is a numeric constant (but then, the Word macro I mention below uses a numeric constant for the range, so maybe Word just doesn't like th one you used).
Then, I got Word to record a macro to print the first two pages of a 6-page doc, and found that I could then get your code to work by making these changes:
replace vFrom and vTo, which I assume are OleVariants, by two integers iFrom and iTo (I don't think that this really matters, just makes it easier to check that the page-range argument is correctly constructed.
replace the code from "vFrom := [...] to Doc.PrintOut by
iFrom := 1; iTo := 2; Doc.PrintOut( Range := wdPrintRangeOfPages, Item := wdPrintDocumentContent, Copies := 1, Pages := IntToStr(iFrom) + '-' + IntToStr(iTo), PageType := wdPrintAllPages, ManualDuplexPrint := False, Collate := True, Background := True, PrintToFile := False, PrintZoomColumn := 0, PrintZoomRow := 0, PrintZoomPaperWidth := 0, PrintZoomPaperHeight := 0 );
Obviously, the arguments this PrintOut includes are ones which the Word macro included. and typically some of them would probably be superfluous.
Thanks for all! MartynA, Jan Doggen, David Heffernan, Mason Wheeler and all others!!
I get solution with the code below:
<pre><code>
procedure TForm1.Button1Click(Sender: TObject);
var
WinWord, Docs, Doc: Variant;
vNomeImpressoraPadraoOriginal : string;
vFrom, vTo : integer;
begin
if (OpenDialog1.Execute) then
begin
try
// Cria objeto principal de controle do Word
WinWord := CreateOleObject('Word.Application');
if (not (VarIsEmpty(WinWord))) then
begin
// Mostra o Word
try
WinWord.Visible := false;
Docs := WinWord.Documents;
// Abre um documento
Doc := Docs.Open(OpenDialog1.FileName);
vFrom := 1;
vTo := 2;
// referência do comando: http://support.microsoft.com/kb/176069/EN-US
Doc.PrintOut(0, 0, '3', '', '1', '2');
// previous code
// Doc.PrintOut(Background := false, Append := false, Range := wdPrintFromTo, OutputFileName := EmptyParam, From := vFrom, To := vTo);
finally
// Fecha o Word
WinWord.ActiveDocument.Close(SaveChanges := 0);
WinWord.Quit;
WinWord := Unassigned;
Docs := Unassigned;
Doc := Unassigned;
end;
end;
showmessage('Fim!');
finally
end;
end;
end;
</code></pre>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.