Word Plugin w/ MFC / ATL - How to modify text contents of active document.

Posted on 2007-04-05
Last Modified: 2013-11-18
I have a MS Word plugin using MFC and ATL.  I know how to get a pointer to the Word active document.  I can save the document, etc., but how can I get access to the text contents of that document to modify it then save it.

Question by:craig20120
  • 4
  • 3
LVL 16

Expert Comment

ID: 18863125
You need to search class Selection or Range
"Use the InsertAfter or InsertBefore method to insert text before or after a Selection or Range object. The following example inserts text at the end of the active document."

It can be /VBA code/
Set doc = ActiveDocument
Set rngRange = _
    doc.Range(doc.Paragraphs(1).Start,  doc.Paragraphs(1).End - 1)
rngRange.InsertAfter _
    " This is now the last sentence in paragraph one."

Author Comment

ID: 18863152
Question...  "It can be VBA code".  Is that a question or a statement??  I don't know how to call VBA code from C++.  


LVL 16

Accepted Solution

AlexNek earned 250 total points
ID: 18863223
It is a statement with notice.
"It can be:"
You don't need to use VBA code in C++. Try only to look into.
Can you see using of ActiveDocument.Range, ActiveDocument.Paragraph(), Range.InsertAfter()?
Independent Software Vendors: 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

ID: 18865159
Yes, I can use those functions.  Not sure of the syntax though.  Do you have the C++ code.
LVL 16

Expert Comment

ID: 18885498
No, I haven't C++ cod.e It can be something like that:

CRange* rngRange = pDocumentt->Range(pDocumentt->Paragraphs(1)->Start(),  pDocumentt->Paragraphs(1)->End() - 1);

rngRange->InsertAfter(" This is now the last sentence in paragraph one.");

Author Comment

ID: 18886025
Does rngRange represent the actual word document itself?   In other words, if I save it to a database, I can read it back out again??  


Author Comment

ID: 18886040
Disregard, the last comment.  Thanks for your help.


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 221
Compile error - linkage specification contradicts earlier specification for 'DllGetClassObject' 6 74
Website Monitoring Tools 3 29
C++ Code Issue 4 37
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

740 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