?
Solved

Word: OleVariant; (Need to call print dialog?)

Posted on 2003-03-22
8
Medium Priority
?
1,989 Views
Last Modified: 2007-12-19
Hi,
Im currently loading a word doc into TOLEContainer and then accesing the word object via the following code.

What I need to do know is call the print method (ie file->print) from within word to show the print dialog.

Delphi6 Pro
Word 2K

Cheers in advance.

////////Demo app

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtnrs, StdCtrls, ExtCtrls, OleServer, Word2000, Menus;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Memo1: TMemo;
    Button5: TButton;
    Button6: TButton;
    MainMenu1: TMainMenu;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    OLE1 : TOLEContainer;
    Word: OleVariant;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//Load doc
procedure TForm1.Button1Click(Sender: TObject);
begin
 Ole1.Parent.Visible := False;
 Ole1.Parent.Parent := nil;
 try
 Ole1.CreateObjectFromFile('c:\guide uk.doc', false);
 Ole1.Modified := False;
 finally
   Ole1.Parent.Parent := Panel1;
   Ole1.Parent.VIsible := True;
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
f: TForm;
begin
 f:= TForm.Create(Self);
 with f do begin
   BorderStyle := bsNone;
   BorderIcons := [];
   Align := alClient;
 end;
 Ole1 := TOleContainer.Create(f);
 Ole1.Parent := f;
 Ole1.Align := alClient;
end;

//KilL!
procedure TForm1.Button2Click(Sender: TObject);
begin
 ole1.DestroyObject;
end;

//Go read only
procedure TForm1.Button3Click(Sender: TObject);
begin
  ole1.DoVerb(ovShow);
  ole1.OleObject.Protect(
    wdAllowOnlyComments, EmptyParam, EmptyParam);
end;



//Hide toolbars
procedure TForm1.Button4Click(Sender: TObject);
var
  Bar, di : OleVariant;
  dl: OleVariant;
  i: integer;
begin
  Word := ole1.OleObject.Application;
  for i := 1 to Word.CommandBars.Count do
  begin
    Bar := Word.CommandBars[i];
    if Bar.Visible then
      { store the information about which bars were
        visible somewhere, so you can set Word back
        to normal when you've finished!
      ... }
      if (VarToStr(Bar.Name) <> 'Menu Bar') then
      begin
        Memo1.Lines.Add(Bar.Name);
        Bar.Visible := False;
      end;
  end;
end;


procedure TForm1.Button5Click(Sender: TObject);
begin

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 OLE1.Close;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
end;

end.


0
Comment
Question by:08718712060
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 

Author Comment

by:08718712060
ID: 8187186
Please, Please
0
 

Expert Comment

by:epaulson
ID: 8187595
Try something like

try
  Word.Dialogs.Item(wdDialogFilePrint).Show;
except
  ShowMessage('Print failed!');
end;

Good luck,

Eric
0
 

Author Comment

by:08718712060
ID: 8187666
Hi epaulson,
Tried that but ended up with an invalid variant oporation!. Getting desperate now and cannot figure it out at all.

Why do we do it?? lol
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:mocarts
ID: 8187794
procedure TForm1.Button2Click(Sender: TObject);
var
  word, dlg: OleVariant;
begin
  ole1.DoVerb(1); // some kind of document activation needed else an error occurs
  word := ole1.OleObject.Application;
  dlg := word.Dialogs.Item(wdDialogFilePrint);
  if dlg.Show(0) = -1 then dlg.Execute;

  // if Word is visible then hide it :(
  if word.Visible then
    word.Visible := False;
end;

as I see you have all Word2000 constants (Word2000.pas in DelphiX\Ocx\Servers directory).
you can additionally browse msdn site for help:
http://msdn.microsoft.com/library/en-us/vbawd10/html/wotocObjectModelApplication.asp

wbr, mo.
0
 

Author Comment

by:08718712060
ID: 8187878
mocarts,
Cheers for your input but unfortunatly I am still receiving "invalid variant oporation"! when I try to call the word.Dialogs.Item(wdDialogFilePrint);.

Any other ideas?
0
 

Expert Comment

by:epaulson
ID: 8187886
I guess I should have mentioned that before executing Word.Dialogs.Item(wdDialogFilePrint).Show that 'Word' must exist as Mocarts has suggested or you will get that error.

In a quick test however, I noticed that the code as stated would always prompt me 2 times when printing to file. If I did:

var
 word, dlg: OleVariant;
begin
 ole1.DoVerb(1); // some kind of document activation needed else an error occurs
 word := ole1.OleObject.Application;
 word.Dialogs.Item(wdDialogFilePrint).Show;
// if dlg.Show(0) = -1 then dlg.Execute;

 // if Word is visible then hide it :(
 if word.Visible then
   word.Visible := False;

It only prompted me once. Not sure why.

Eric
0
 

Accepted Solution

by:
epaulson earned 1400 total points
ID: 8187901
I guess I should have mentioned that before executing Word.Dialogs.Item(wdDialogFilePrint).Show that 'Word' must exist as Mocarts has suggested or you will get that error.

In a quick test however, I noticed that the code as stated would always prompt me 2 times when printing to file. If I did:

var
 word, dlg: OleVariant;
begin
 ole1.DoVerb(1); // some kind of document activation needed else an error occurs
 word := ole1.OleObject.Application;
 word.Dialogs.Item(wdDialogFilePrint).Show;
// if dlg.Show(0) = -1 then dlg.Execute;

 // if Word is visible then hide it :(
 if word.Visible then
   word.Visible := False;

It only prompted me once. Not sure why.

Eric
0
 

Author Comment

by:08718712060
ID: 8188029
Eric,
Worked a dream. A huge thank you to both yourself and mocarts for your expert help on this desperate query.

Thanks guys

Stuart
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question