LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Setting SpaceAfter property with Word ActiveX

Daniel,

 

I use the document handle for setting other parameters just before the call to the SetLineSpacing function, so I know the handle is valid.  I'm also sending the enumerated constant for single space (although that doesn't matter because the error is thrown before that constant is used).  I managed to get around the problem by using the SetProperty function instead.  My code now looks like this:

 

/********************
** GenerateLogFile **
*********************/
void GenerateLogFile(void)
{
    CAObjHandle AppHandle = 0;
    CAObjHandle CurrentSelectionHandle;
    CAObjHandle ParagraphFormatHandle;
    
    RPTERR(WordRpt_ApplicationNew(VTRUE, &AppHandle));
    

        // note: DocHandle is declared at file scope
    RPTERR(WordRpt_DocumentNew (AppHandle, &DocHandle));

    RPTERR(Word_GetProperty (AppHandle,
                             NULL,
                             Word_ApplicationSelection,
                             CAVT_OBJHANDLE,
                             &CurrentSelectionHandle));

    RPTERR(Word_GetProperty (CurrentSelectionHandle,
                             NULL,
                             Word_SelectionParagraphFormat,
                             CAVT_OBJHANDLE,
                             &ParagraphFormatHandle));

// generates an error - waiting for feedback from NI Forum
// replaced with following call to SetProperty
//    RPTERR(WordRpt_SetReportLineSpacing (DocHandle, WRConst_Single));
    
    RPTERR(Word_SetProperty (ParagraphFormatHandle, NULL, Word_ParagraphsLineSpacingRule, CAVT_SHORT, WordConst_wdLineSpaceSingle));
    RPTERR(Word_SetProperty (ParagraphFormatHandle, NULL, Word_ParagraphsSpaceBefore, CAVT_FLOAT, 0.0));
    RPTERR(Word_SetProperty (ParagraphFormatHandle, NULL, Word_ParagraphsSpaceAfter, CAVT_FLOAT, 0.0));

 

    // generate report content       

    CA_DiscardObjHandle(AppHandle);
    CA_DiscardObjHandle(DocHandle);
    CA_DiscardObjHandle(CurrentSelectionHandle);
    CA_DiscardObjHandle(ParagraphFormatHandle);
    
    DocHandle = 0;
} /* GenerateLogFile */

0 Kudos
Message 11 of 15
(780 Views)

Tony,

 

I don't see in your code where you use the document handle before the SetReportLineSpacing call. Is it elsewhere in the code? I know you said you declare it at file scope, and I see the DocumentNew call to assign the document handle, but I can't see anywhere else that it's used. Does the DocumentNew function return an error code, or does it seem to work properly?

0 Kudos
Message 12 of 15
(773 Views)

Daniel,

 

Sorry, I mispoke.  What I meant to say was that I use the DocHandle to retrieve the other collection handles, and those all work fine.  And no, the DocumentNew function does not return an errror.

 

Tony G.

0 Kudos
Message 13 of 15
(770 Views)

AHAH!!!

 

In looking over the WordRpt_SetReportLineSpacing code, it seemed odd that it takes a document handle since the other formatting functions take selection or paragraph handles.  So I ran a couple of quick experiments and discovered that if you pass a selection handle to WordRpt_SetReportLineSpacing, it works correctly.  Apparently the function documentation is wrong.  It says to pass a document handle, but it really needs a selection handle.  Here's the final (working, error-free) code:

 

/********************
** GenerateLogFile **
*********************/
void GenerateLogFile(void)
{
    CAObjHandle AppHandle = 0;
    CAObjHandle CurrentSelectionHandle;
    CAObjHandle ParagraphFormatHandle;
    
    RPTERR(WordRpt_ApplicationNew(VTRUE, &AppHandle));
    
    RPTERR(WordRpt_DocumentNew (AppHandle, &DocHandle));

    RPTERR(Word_GetProperty (AppHandle,
                             NULL,
                             Word_ApplicationSelection,
                             CAVT_OBJHANDLE,
                             &CurrentSelectionHandle));

    RPTERR(Word_GetProperty (CurrentSelectionHandle,
                             NULL,
                             Word_SelectionParagraphFormat,
                             CAVT_OBJHANDLE,
                             &ParagraphFormatHandle));

    /*
        The documentation for the following function indicates that the first
        parameter should be the document handle.  However, that generates an
        ActiveX error.  Through experimentation I discovered that if you pass
        the selection handle, it works correctly.
    */  
    RPTERR(WordRpt_SetReportLineSpacing (CurrentSelectionHandle, WRConst_Single));
    
    RPTERR(Word_SetProperty (ParagraphFormatHandle, NULL, Word_ParagraphsSpaceBefore, CAVT_FLOAT, 0.0));
    RPTERR(Word_SetProperty (ParagraphFormatHandle, NULL, Word_ParagraphsSpaceAfter, CAVT_FLOAT, 0.0));

    

    // generate report content

    CA_DiscardObjHandle(AppHandle);
    CA_DiscardObjHandle(DocHandle);
    CA_DiscardObjHandle(CurrentSelectionHandle);
    CA_DiscardObjHandle(ParagraphFormatHandle);
    
    DocHandle = 0;
} /* GenerateLogFile */

0 Kudos
Message 14 of 15
(767 Views)

Tony,

 

That's very interesting--I'm glad your code is working now, though! We'll definitely take a look at that function and update the documentation if necessary.

0 Kudos
Message 15 of 15
(761 Views)