This article was previously published under Q316207
For a Microsoft Visual C++ 6.0 version of this article, see 238611.
IN THIS TASK
• | SUMMARY
|
||||||
• | REFERENCES |
On this Page
SUMMARY | ||
REFERENCES |
Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.
SUMMARY
This step-by-step article describes how to embed a Word document in a View object in a single-document interface (SDI) Microsoft Foundation Classes (MFC) application by using Visual C++ .NET.
back to the top
back to the top
back to the top
back to the top
Create an MFC Application that Embeds a Word Document
The following steps demonstrate how to embed a Word document and automate the document to add data to a cell.1. | Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Under Project types click Visual C++ Projects, and then click MFC Application under Templates. Name the project Embed_Word. | ||||||||||||||
2. | When the MFC Application Wizard appears, follow these steps:
|
||||||||||||||
3. | Add interfaces from the Word object library. To do this, follow these steps:
|
||||||||||||||
4. | Add the following line to Cntritem.h as a public member function of the CEmbed_WordCntrItem class:
|
||||||||||||||
5. | Add the GetIDispatch method to Cntritem.cpp, as follows:
|
||||||||||||||
6. | Add the following line to Embed_wordview.h as a public method of the CEmbed_WordView class:
|
||||||||||||||
7. | Add the following lines to Embed_wordview.cpp:
|
||||||||||||||
8. | Replace the code for CEmbed_WordView::OnInsertObject in Embed_wordview.cpp with the following: NOTE: EmbedAutomateWord is merely a special case of OnInsertObject, which allows the user to select from a list of available OLE objects to insert into the application. You will override this behavior because it is not needed for this demonstration.
|
Test the Application
1. | Press F5 to build and run the application. |
2. | On the Edit menu of the application, click Insert New Object. |
3. | Examine the results. A new Word document is embedded into the View object, with the text "Hello, World!". |
Troubleshooting
If you add class wrappers for the Word object library by using the File option in the Add Class From TypeLib Wizard, you may receive an error message when you browse to the object library. To avoid this problem, type the full path and file name for the object library instead of browsing to the file. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:311408 BUG: 'Read-Only' Warning When Adding MFC Class From Library
back to the top
REFERENCES
For more information, see the following Microsoft Developer Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficedev.asp
For additional information about Word Automation, see the following Knowledge Base articles:
http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficedev.asp
308338 How To Automate Word To Perform a Mail Merge From C++ .NET and MFC
309294 How To Handle Events for Microsoft Word by Using C++ .NET and MFC