Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

CNiWordDocument utilities

Hello,
is it possible with CNiWordDocument objects to look for a string within the document or to make other word processing common operations? How to use field properties like "fieldsummary" or other?

thank you for your answer
0 Kudos
Message 1 of 11
(4,776 Views)
Yes, but you have to write a bit of code. CNiWordDocument is basically a wrapper around the Word Document interface. One option is to call CNiWordDocument::GetDispatch to get the Document IDispatch pointer, then use Word automation interfaces to automate the operations that you're interested in. Another option is to use the CNiWordDocument InvokeHelper, GetProperty, and SetProperty methods to automate the Word document via the CNiWordDocument class. The first option is easier and is what I recommend.

- Elton
0 Kudos
Message 2 of 11
(4,760 Views)
thank you for your answer. But how to use Word Automation Interfaces once the CNiWordDocument::GetDisp function called? Must I configure Word?
Can you show an example in which a string is looked for within a CNiWordDocument document?

Thank you very much for your precious help
0 Kudos
Message 3 of 11
(4,757 Views)
First, add the following to your stdafx.h (this assumes Office XP; the paths will change if you have an earlier version of Office):


#include "oleacc.h"
#include "atlbase.h"

#pragma warning(push)
#pragma warning(disable: 4278)

#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office10\\mso.dll" \
rename_namespace("OfficeXP")
using namespace OfficeXP;

#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.olb" \
rename_namespace("VBE6")
using namespace VBE6;

#import "C:\\Program Files\\Microsoft Office\\Office10\\MSWORD.olb" \
rename("ExitWindows", "WordExitWindows") \
named_guids, \
rename_namespace("MSWord")
using namespace MSWord;

#pragma warning(pop)


The gist of what I was describing is that you can create CNiWordApplication and CNiWordDocument objects, get the LPDISPATCH via the GetDispatch method, then create an ATL CComQIPtr with the LPDISPATCH and the corresponding interface type as generated by #import and use this to get to the Word automation functionality. For example, a very simple example would be automating a print preview on the document:


CNiWordApplication app;
CNiWordDocument document = app.CreateDocument();
document.AppendLine(_T("Line 1"));

CComQIPtr<_Document> spDocument(document.GetDispatch(false));
spDocument->PrintPreview();

// ...


Regarding your question about finding a string in the Word document, I'm not very familiar with the Word automation object model, so this may not be the best way to do it, but here's a short example that I think will at least get you on the right track:


CNiWordApplication app;
CNiWordDocument document = app.CreateDocument();

document.AppendLine(_T("Line 1"));
document.AppendLine(_T("Line 2"));
document.AppendLine(_T("Line 3"));
document.MoveCursorUp(3);

CComQIPtr<_Application> spApplication(app.GetDispatch(true));
SelectionPtr selection = spApplication->Selection;
FindPtr find = selection->Find;

find->Text = _bstr_t(_T("Line 2"));
VARIANT_BOOL success = find->Execute();


Hope this helps.

- Elton
Message 4 of 11
(4,740 Views)
thank you for your help, I tried and it works.
But how to get the whole utilities? Is it possible to realize each operation possible with Word like to reach a certain tab?
I didn't find explanation in the help section.

thank you very much.
0 Kudos
Message 5 of 11
(4,731 Views)
Yes. To find the help, start Word, go to the Help menu, click "Microsoft Office Word Help," click the "Table of Contents" link in the Word Help pane, then look at the bottom of the table of contents for a book titled "Microsoft Word Visual Basic Reference." This contains the help for the Word automation object model. You can use these same objects in Visual C++ as shown above.

- Elton
0 Kudos
Message 6 of 11
(4,715 Views)
I managed to go into a tab in a word document but i can't get the string of the current cell. I looked some macros in visual basic but in C++ i don't have all the same properties!!! Have you got an idea to get the string of a cell?
thank you.
0 Kudos
Message 7 of 11
(4,714 Views)
The Word automation API is a COM interface and the same COM interfaces are used in Visual C++ as Visual Basic, so you should have access to the same properties in Visual C++ as you do in Visual Basic. What is the Visual Basic code that does what you're looking for?

- Elton
0 Kudos
Message 8 of 11
(4,698 Views)
hello,

I found in a a macro this code :
to get text in a cell : celltext = ActiveDocument.Tables(n°Tab).Cell(1,1)
In C++, I can't get a string but pointers like TablesPtr or CellsPtr. Moreover, i didn't find the function Tables with arguments.

Did I missed a concept?
what would be the equivalent on C++ of what i wrote above?
0 Kudos
Message 9 of 11
(4,699 Views)
Here's the equivalent C++ code:


// Create an example document
CNiWordApplication app;
CNiWordDocument document = app.CreateDocument();

// Create an example table.
CNiWordTable table = document.AddTable(2, 2);
table.WriteToCell(1, 1, _T("Cell 1"));
table.WriteToCell(1, 2, _T("Cell 2"));
table.WriteToCell(2, 1, _T("Cell 3"));
table.WriteToCell(2, 2, _T("Cell 4"));

// Retrieve the text of the first cell of the table.
CComQIPtr<_Application> spApp(app.GetDispatch(false));
_DocumentPtr spDocument = spApp->ActiveDocument;
TablesPtr spTables = spDocument->Tables;
TablePtr spTable = spTables->Item(1);
CellPtr spCell = spTable->Cell(1, 1);
RangePtr spRange = spCell->Range;
_bstr_t cellText = spRange->Text;

// Display the text in a message box ("Cell 1").
MessageBox(cellText);


You can get the text directly from the cell in VB rather than having to access the Range and then the Text properties because Range is the default property of Cell, and Text is the default property of Range.

- Elton
Message 10 of 11
(4,683 Views)