?
Solved

Delphi 2009 word 2007

Posted on 2009-05-11
5
Medium Priority
?
2,172 Views
Last Modified: 2012-05-06
In the uses part i have word2000 (is there a 2007 library?)
i try to open an docx document. It compiles ok, but i get type mismatch at line:
Word.Documents.Open(FileName,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);

what's wrong?
unit D2009Word2007U;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, word2000,ExtCtrls,ComObj, ActiveX;
 
type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Timer1Timer(Sender: TObject);
var
  Word: _Application;
  AppWasRunning: boolean; // tells you if you can close Word when you've finished
  Unknown: IUnknown;
  Result: HResult;
  FileName,EmptyParam: OleVariant;
begin
  timer1.Enabled:=false;
  AppWasRunning := False;
 
  {$IFDEF VER120}      // Delphi 4
  Result := GetActiveObject(CLASS_Application_, nil, Unknown);
  if (Result = MK_E_UNAVAILABLE) then
    Word := CoApplication_.Create
 
  {$ELSE}              // Delphi 5
  Result := GetActiveObject(CLASS_WordApplication, nil, Unknown);
  if (Result = MK_E_UNAVAILABLE) then
    Word := CoWordApplication.Create
  {$ENDIF}
 
  else begin
    { make sure no other error occurred during GetActiveObject }
    OleCheck(Result);
    OleCheck(Unknown.QueryInterface(_Application, Word));
    AppWasRunning := True;
  end;
  Word.Visible := True;
  FileName := 'c:\file.docx';
  Word.Documents.Open(FileName,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
end;
 
end.

Open in new window

0
Comment
Question by:MdJ70
[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
  • 2
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
developmentguru earned 1000 total points
ID: 24364018
DOCX is the new format supported by office 2007.  If you have not downloaded the patch for older versions of word it will not allow DOCX.  One thing I would try is to open a .DOC or a .TXT file with the same code and see if it works.  This will identify for you if it is the DOCX itself causing the issue.

As for there being a new library for 2007 office, you can import it using the component | Import Component - Import Active X - choose the 2007 automation library, fill in where you want it to create the files.  It will create the Delphi interface files for you.  One word of caution on this approach.  It will limit your code so it will require Office 2007 to be installed to work.  If this is an application that is only going to be used in your office and everyone is on Office 2007 then it should be no problem.  If it is a commercial application, and you want to support multiple versions, then you have a great deal more work to do.

Let me know how the testing goes.
0
 

Author Comment

by:MdJ70
ID: 24364196
doc gives same type mismatch error.
the error seems to be in the Word.Documents.Open part....

2 questions:
1)what is the name of the office/word 2007 automation library?
(i can't seem to find it in Import component-import activeX. office 2007 is installed on my system though.)
2) what is the name of the office 2007 automation libray in the uses clause? word2007?

thanks.
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24368007
Word Object Library is:
Office 12\MSWORD.OLB

Microsoft Office 12.0 Object Library (Office shared functionality)
office.dll

See this page which lists them all:
http://msdn.microsoft.com/en-us/library/15s06t57.aspx

HTH
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 24369021
There was a similar office automation question recently... the last parameter needed to be something other than null... let me see if I can find more info on that.

In the mean time, there is a simpler way to try to tackle it.  You can request the default word application.  This will return the office 2007 if it is installed or office 2003 or whatever is installed.  You can then call the Open with just the file name.  The down side of this is that you loose the tips on the parameters.  The upside is that it can make things much easier versus multiple versions you try to run against.  Here is a link with some code.

http://www.swissdelphicenter.ch/torry/showcode.php?id=1691
0
 

Author Closing Comment

by:MdJ70
ID: 31580089
thanks
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

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
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…
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 shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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