Solved

Problem with Word 2010 in Delphi XE7 Pro

Posted on 2015-02-21
8
498 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
[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
  • 2
8 Comments
 
LVL 27

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
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!

 

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 27

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

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

739 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