Solved

Problem with Word 2010 in Delphi XE7 Pro

Posted on 2015-02-21
8
480 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 26

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

937 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