Solved

Problem with Word 2010 in Delphi XE7 Pro

Posted on 2015-02-21
8
478 Views
Last Modified: 2015-05-30
Hello Experts. I really hope you can help me with this one.

I have some code that worked fine in Delphi 2006 to open a Word .doc file via OLE Automation and now I want to update it to Delphi XE7 and Word 2010.

I set up the 16 parameters ready for the Open() function that is given in the Word2010.pas file (derived from the type library) then invoke it and get an EVariantBadVarTypeError Exception with message: "Invalid variant type".

I've followed the specification for the parameters carefully (I think) and can't work out what's wrong.

Can you help?

Maximum points as this is really urgent and important.

Here's the procedure:

procedure TDocPDF.ExOpenDocument(sDocFile :string);
var
    WordApp : _ApplicationDisp;
      DocumentTemplate  : _DocumentDisp;
      DocsCollection    : DocumentsDisp;
    vTemplate : OleVariant;
    vVisible : OleVariant;
    vOpenFormat : OleVariant;
   vConfirmConversions,
            vReadOnly ,
      vAddToRecentFiles,
            vPasswordDocument,
            vPasswordTemplate,
                  vRevert,
                  vWritePasswordDocument,
                  vWritePasswordTemplate ,
                  vXMLTransform : OleVariant;
      vNewTemplate : OleVariant;
      vDocumentType : OleVariant;
      vEncoding : OleVariant;
      vOpenAndRepair : OleVariant;
      vDocumentDirection : OleVariant;
      vNoEncodingDialogue : OleVariant;


begin
// load a copy of Word 2010:
      CoInitialize(nil);
      WordApp := (CoWordApplication.Create as _ApplicationDisp);
      WordApp.Visible := bVisible;
      DocsCollection := WordApp.Documents as DocumentsDisp;

// set up the parameters for the .Open command:
   vTemplate := sDocFile ;
   vVisible := False;
      vConfirmConversions:=false;
            vReadOnly :=False;
      vAddToRecentFiles:=False;
            vPasswordDocument:=EmptyParam;
            vPasswordTemplate :=EmptyParam;
                  vRevert:=False;
                  vWritePasswordDocument:=EmptyParam;
                  vWritePasswordTemplate:=EmptyParam;
                  vOpenFormat:=wdOpenFormatAuto;
   vEncoding:=EmptyParam;
   vOpenAndRepair:=EmptyParam;
   vDocumentDirection:=EmptyParam;
   vXMLTransform:=EmptyParam;

try
   DocumentTemplate := DocsCollection.Open(vTemplate,
                              vConfirmConversions,
          vReadOnly,
          vAddToRecentFiles,
          vPasswordDocument,
          vPasswordTemplate,
          vRevert,
          vWritePasswordDocument,
          vWritePasswordTemplate,
          vRevert,
          vOpenFormat,
          vEncoding,
          vVisible,
          vOpenAndRepair,
          vDocumentDirection,
          vXMLTransform) as _DocumentDisp;
except on e: exception do begin
   Application.MessageBox(PChar('Problem in ''ExOpenDocument'':'+CRLF+E.message),
                                                      PChar('ExOpenDocument'), MB_OK);
end;
end;

Thank you very much.

Steve
0
Comment
Question by:SteveFarndon2000
  • 4
  • 2
8 Comments
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 40623559
try this....
...
WordApp := (CoWordApplication.Create as _ApplicationDisp);
WordApp.Visible := bVisible;
 WordApp.Documents.Open(sDocFile, false, false); 
...

Open in new window

... emptyparam values are not necessary
0
 

Author Comment

by:SteveFarndon2000
ID: 40623973
This doesn't compile. The compiler says 'not enough actual parameters'.

I think you are getting early-binding and late-binding confused. If you start with:

  WordApp := CreateOleObject('Word.Application');

then

WordApp.Documents.Open(sDocFile, false, false);

will work because  you can invoke methods without the 'optional' parameters, yes?
0
 

Author Comment

by:SteveFarndon2000
ID: 40624072
Just to be clear, I'm interested only in getting the

WordApp := (CoWordApplication.Create as _ApplicationDisp);

route to work, not 'CreateOleObject' . Thanks.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:SteveFarndon2000
ID: 40624112
Correction:
I made a typo in the method call statement in the questiont.  It should read :
   DocumentTemplate := DocsCollection.Open(vTemplate,
                              vConfirmConversions,
          vReadOnly,
          vAddToRecentFiles,
          vPasswordDocument,
          vPasswordTemplate,
          vRevert,
          vWritePasswordDocument,
          vWritePasswordTemplate,
          vOpenFormat,
          vEncoding,
          vVisible,
          vOpenAndRepair,
          vDocumentDirection,
          vNoEncodingDialogue,
          vXMLTransform) as _DocumentDisp;

This was the call that gave the error.
My apologies for any confusion.
Thank you.
0
 
LVL 25

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 500 total points
ID: 40624387
try to remove forced interface casting
var
  WordDoc: _Document;
  WordApp: _Application;
  Docs: Documents;
begin
  WordApp := CoWordApplication.Create;
  WordApp.Visible := True;
  Docs := WordApp.Documents;
//fill EmptyParam variables here
...
  vPasswordDocument:=Variants.EmptyParam;
...
  WordDoc := Docs.Open(...);

Open in new window

0
 

Accepted Solution

by:
SteveFarndon2000 earned 0 total points
ID: 40632025
You are getting there, Sinisa. I've researched other places and done some tests which point to a solution based on developing your idea.

Better to start with the main vars as what I call 'top level' types used in the Word2010.pas unit - WordApplication and WordDocument. At the top of that unit it says:-

//   Hint: Symbol 'Application' renamed to 'WordApplication'
//   Hint: Symbol 'Document' renamed to 'WordDocument'

Open in new window


This idea seems more in line with the what the Word2010.pas unit is driving at (I think!).

You can call .Open(...) with non-OleVariant type parameters so no need for...
...
vPasswordDocument:=Variants.EmptyParam;
...

Open in new window


We end up with:
...
var
  WordDoc:  WordDocument;
  WordApp:  WordApplication;
  Docs: Documents;
 sDocFile: string;
...
begin
  WordApp := CoWordApplication.Create;
  WordApp.Visible := True;
  Docs := WordApp.Documents;
...
sDocFile:='c:\config.doc';
Doc := Docs.Open( sDocFile, false, false, false,
     EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
     wdOpenFormatAuto,
      EmptyParam,
      true, 
    EmptyParam,
     EmptyParam, EmptyParam, EmptyParam);
...

Open in new window


Simple.
Anything to add?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now