Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2015
  • Last Modified:

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

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
08718712060
Asked:
08718712060
  • 4
  • 3
1 Solution
 
08718712060Author Commented:
Please, Please
0
 
epaulsonCommented:
Try something like

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

Good luck,

Eric
0
 
08718712060Author Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mocartsCommented:
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
 
08718712060Author Commented:
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
 
epaulsonCommented:
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
 
epaulsonCommented:
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
 
08718712060Author Commented:
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!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now