LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Need an example of FindExecute (Word Automation)

I'm using Word Automation to automate report generation from a LabWindows/CVI application.
I've got most of the Word Automation working properly, thanks to the NI provided examples, but I can't seem to get the Word_FindExecute function to work properly. I get an error message: "Member not found!", which I believe indicates I'm not using an appropriate object, although I am using a FindObject.

Any assistance (an example?) would be appreciated.

DMattern
raf5de6k@yahoo.com
0 Kudos
Message 1 of 5
(3,664 Views)
I tried it and had no problem. I can't be sure why you are seeing what you are seeing, but I would suspect an invalid or incorrect object handle. Here is the code I used to get it to work. It returned a VTRUE (-1) value telling me it found the search string.

//Get the number of characters in the document
Word_GetProperty (docHandle, NULL, Word_DocumentCharacters, CAVT_OBJHANDLE, &charHandle);
Word_GetProperty (charHandle, NULL, Word_CharactersCount, CAVT_LONG, &numChars);

//Select the Range to search (char 0 to numChars)
Word_DocumentRange (docHandle, NULL, CA_VariantInt(0), CA_VariantInt(numChars), &rangeHandle);

//Get a handle to the find object
Word_GetProperty (rangeHandle, NULL, Word_RangeFind, CAVT_OBJHANDLE, &findHandle);

//C
lear the Find Formatting
Word_FindClearFormatting (findHandle, NULL);

//Search for string "test"
CA_VariantSetCString (&vStr, "test");
status = Word_FindExecute (findHandle, NULL, vStr,
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (),
CA_DefaultValueVariant (), &found);


Don't forget to discard your handles when you finish with them with CA_DiscardObjHandle.

Good luck,

Chris Matthews
National Instruments
Message 2 of 5
(3,664 Views)
Sorry, this doesn't work for me.

Your Word_FindExecute has 17 parameters? The version of Word8 that I'm using only requires 14. After making these changes to your code, adding the declarations and running it with with the worddemo.c example, I get an error, from Word_FindExecute of "invalid handle".
I have just modified the worddemo.c that comes with LabWindows/CVI in the samples/activex/word directory.
The modification includes a pushbutton to run FindExecute.

int WriteReport(struct ReportData *d)
{
HRESULT error = 0;
CAObjHandle docHandle;
WordObj_Characters charHandle;
long numChars;
WordObj_Range rangeHandle;
WordObj_Find findHandle;
VARIANT vStr;
VBOOL found;
HRESULT status;

Word_GetProperty (docHandle, NULL, Word_Doc
umentCharacters, CAVT_OBJHANDLE, &charHandle);
Word_GetProperty (charHandle, NULL, Word_CharactersCount, CAVT_LONG, &numChars);
Word_DocumentRange (docHandle, NULL, CA_VariantInt(0), CA_VariantInt(numChars), &rangeHandle);
Word_GetProperty (rangeHandle, NULL, Word_RangeFind, CAVT_OBJHANDLE, &findHandle);
CA_VariantSetCString (&vStr, "e");
caErrChk(Word_FindExecute (findHandle, NULL, vStr, CA_DefaultValueVariant (),
CA_DefaultValueVariant (),CA_DefaultValueVariant (),CA_DefaultValueVariant (),
CA_DefaultValueVariant (),CA_DefaultValueVariant (),CA_DefaultValueVariant (),
CA_DefaultValueVariant (),CA_DefaultValueVariant (),CA_DefaultValueVariant (), &found));
Error:
if (error < 0) ReportAppAutomationError (error);
return(error);
}
0 Kudos
Message 3 of 5
(3,664 Views)
Yea, I'm using a newer version of Word so there are 4 more optional parameters to the FindExecute function.

I found a problem in your code. You are redeclaring the docHandle as a local variable. In the example, the docHandle is a global. If you put in this local declaration, you won't be using the right handle. I put your code in my Excel demo example and it worked without the CAObjHandle docHandle line. I am using CVI 6.0 and Office 2000.

Chris
0 Kudos
Message 4 of 5
(3,664 Views)
Well that fixed the example. Thanks, I get VTRUE (-1) as a result now. I think with this example will be able to get my findExecute code to work. The problem I was having had to do with the generation of the findHandle object.

Thanks Chris.

By the way, if anyone is interested, using find and replace in Word automation is a nice way to generate reports using templates, which separates the report data from the report format, kind of like a style sheet. It allows you to change the report format by changing the template, without having to recompile the LabWindows program.
0 Kudos
Message 5 of 5
(3,664 Views)