Reading and Writing MS Word files
Harry Hahne -- hahne@epas.utoronto.ca
Thursday, January 18, 1996
I have the need to read a Microsoft Word file and replace certain
tokens in the text with formatted material from my database (this is a
proprietary database written in C++). I do not care about working
with any imbedded OLE objects, as long as they remain undistburbed
after I modify the Word document. I do need to include text
attributes such as italics and superscripts however.
Can anyone direct me to information or sample code on how to do this?
I have been browsing the MSDN and there seem to be some ideas about
doing this with Visual basic, but not C/C++ or MFC. I gather than
Word files are OLE compound documents, but I need more information
about how to manipulate this type of file and the specific details of
Word compound documents. Any suggestions will be appreciated.
If there is another list or USENET group which would be a good place
to ask this, please let me know.
Harry Hahne
hahne@epas.utoronto.ca
Mike Blaszczak -- mikeblas@interserv.com
Saturday, January 20, 1996
[Mini-digest: 4 responses]
On Thu, 18 Jan 1996, "Harry Hahne" wrote:
>I have the need to read a Microsoft Word file and replace certain
>tokens in the text with formatted material from my database (this is a
>proprietary database written in C++). I do not care about working
>with any imbedded OLE objects, as long as they remain undistburbed
>after I modify the Word document. I do need to include text
>attributes such as italics and superscripts however.
>Can anyone direct me to information or sample code on how to do this?
You should do this by writing a Word BASIC macro, or by using OLE to drive an
instance of Word for Windows.
The Word for Windows file format is subject to change! It will _necessarily_
change with each version of Word for Windows. If, at the slightest whim, the
Word developers think a different file format would be smaller, faster, or
more robust, they'll change the format. The file format will almost
necessarily change with each version of Windows. Word for Windows bases its
file format on OLE storage mechanisms, which are defined by the system. If
the system changes the way OLE storage works, the file format will change.
If you still want to meddle where you don't belong, you should write to
Microsoft Developer Support and ask for the "Office File Format". You'll
need to explain why you want this format, and why normal programmatic
interfaces (which Microsoft has spent millions developing, testing, and
documenting) for the products aren't good enough for you. You'll then need
to sign an non-disclosure agreement, and probably a couple of other
documents. You'll then, maybe, be sent the documentation for the format.
Information on this service can be found in the Microsoft Knowledge
Base--apparently, you should _search_ instead of just browsing. If you still
can't find the article, please let me know and I'll post it here.
.B ekiM
--
TCHAR szDisc[] = _T("These words are my own; I do not speak for Microsoft.");
-----From: Chris Krahe
It sounds like you may want to control Word through its OLE automation
server, Word.Basic.
To get started, obtain a copy of the Word type library (.tlb) appropriate
for your version of Word (if you need this file for Word 6.0a, email me
directly). Then, create a new class with ClassWizard from this file, naming
it something like "CWordBasic" or whatever. ClassWizard creates your
CWordBasic as a descendant of COleDispatchDriver. You may want to remove
the ExitWindows member function as it confuses the compiler about the macro
by the same name.
In your project, be sure you have oledisp.h included (usually in stdafx.h)
and be sure you call AfxInitOle, usually from your CWinApp's InitInstance().
Instantiate a CWordBasic object and call its CreateDispatch member, passing
"Word.Basic".
You are now ready to drive Word.
If you are familiar with WordBasic, the rest should be easy. If not, look
at the Word Developer's Kit, included on the Developer's CD if you have it.
You can also learn by recording/writing macros in Word itself.
Hope this has been helpful...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris Krahe, ckrahe@clark.net
http://www.clark.net/pub/ckrahe
http://www.kcm-inc.com (my employer)
"A car does not the driver make", cpk`88
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----From: csampale@exis.com
There is now a library that allows your program to access compound
documents relatively easily (much more easily than rolling your own
stuff). I believe it comes as an OLE control, so it's usable from
anything which can embed an OLE control...
I don't remember exactly which magazine I read about it, but it was a
very recent issue. It was in either the recent columns PC Mag had on
available controls (spread out over two months), Dr. Dobbs, or
Windows Programmer Journal...
-----From: gtran@ix.netcom.com (Nam H. Tran )
Try to look for Don Box's article (in the OLE Q&A section) in MSDN (at
least from the July '95 edition). It helped me plenty in terms of
creating an OLE client to Word Basic using Visual C++ and MFC.
Gus Tran
gtran@ix.netcom.com
| Вернуться в корень Архива
|