Solved

Problem with Word 2010 in Delphi XE7 Pro

Posted on 2015-02-21
8
506 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

632 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