Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with Word 2010 in Delphi XE7 Pro

Posted on 2015-02-21
8
Medium Priority
?
522 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
6 Comments
 
LVL 28

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
Technology Partners: 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 28

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Suggested Courses

783 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