LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Setting SpaceAfter property with Word ActiveX

I am using the wordreport and Microsoft Word Object 9.0 Object Library instruments to generate a report.  I would like set the paragraph Space Before and Space After properties before generating my report.  In Word this is part of the paragraph formatting options.  I want to set both to 0 for my report.  According to the documentation on MSDN, these properties are part of the Paragraph collection in the ActiveX (the following is from http://msdn.microsoft.com/en-us/library/office/bb208789%28v=office.12%29.aspx😞

 

Switching the space before a paragraph between 12 points and none

The following example toggles the space-before formatting of the first paragraph in the selection. The macro retrieves the current space before value, and if the value is 12 points, the space-before formatting is removed (the SpaceBefore property is set to zero). If the space-before value is anything other than 12, the SpaceBefore property is set to 12 points.

Sub ToggleParagraphSpace()
    With Selection.Paragraphs(1)
        If .SpaceBefore <> 0 Then
            .SpaceBefore = 0
        Else
            .SpaceBefore = 6
        End If
    End With
End Sub

 

I cannot find a function in the ActiveX library that sets this property. There are several Paragraph and Selection formatting functions, but none for Space Before or Space After.  Is it possible to set these properties through the ActiveX control?

 

Tony G.

 

0 Kudos
Message 1 of 15
(4,043 Views)

Hey Tony,

 

I opened up the Word 9.0 Object Library and then looked at the function panel for the Word_SetProperty function. For PropertyID, SpaceBefore and SpaceAfter are both options. If you're writing the function as text, the parameter would be Word_ParagraphFmtSpaceBefore or Word_ParagraphFmtSpaceAfter.

 

Is this basically what you were looking for, or was there a problem with using those functions? If you're still having trouble, a short code snippet of that section would be useful for troubleshooting. I hope it works for you!

0 Kudos
Message 2 of 15
(4,030 Views)

Daniel,

 

Thanks!  That's exactly what I was looking for.  I didn't realize it was hidden in the SetProperty function.  It seems odd that the ActiveX library includes paragraph formatting functions like ParagraphFmtTabIndent and ParagraphFmtSpace1, but it doesn't have explicit functions for SpaceBefore and SpaceAfter. 

 

For the benefit of anyone else who might be following this thread, I had to add a few functions to get some additonal ActiveX collection handles.  Here's what the final code looks like:

 

/********************
** GenerateLogFile **
*********************/
void GenerateLogFile(void)
{
    CAObjHandle DocHandle;  
    CAObjHandle AppHandle;
    CAObjHandle CurrentSelectionHandle;
    CAObjHandle ParagraphFormatHandle;
    
    WordRpt_ApplicationNew(VTRUE, &AppHandle);
    
    WordRpt_DocumentNew (AppHandle, &DocHandle);
    
    WordRpt_SetReportLineSpacing (DocHandle, 1);
    
    Word_GetProperty (AppHandle,
                      NULL,
                      Word_ApplicationSelection,
                      CAVT_OBJHANDLE,
                      &CurrentSelectionHandle);

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

    Word_SetProperty (ParagraphFormatHandle, NULL, Word_ParagraphsSpaceBefore, CAVT_FLOAT, 0.0);
    Word_SetProperty (ParagraphFormatHandle, NULL, Word_ParagraphsSpaceAfter, CAVT_FLOAT, 0.0);

 

    // create report using functions like WordRpt_AppendLine and WordRpt_InsertImage       

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

I have one last problem.  I've run this program on three different PCs.  All three are running the same versions of Windows 7 and Word 2007.  And yet on one of the three PCs, the WordRpt_InsertImage function doesn't work.  All the other text in the log file is present, but the bitmaps don't get inserted.  I thought that maybe there was a difference in the ActiveX control versions, but I can't find a way to determine what version of the ActiveX is intalled on each machine.  Or is there something else I should be looking at?

 

Tony G.

0 Kudos
Message 3 of 15
(4,026 Views)

Hey Tony,

 

Glad the paragraph formatting stuff works for you. As for the WordRpt_InsertImage function, I see from the CVI Help (http://zone.ni.com/reference/en-XX/help/370051V-01/toolslib/functionreference/cviwordrpt_insertimage...) it looks like that function will return an error code (negative number if the function failed). It might be useful to insert some code to print that error code so we can see what the problem might be. Presumably, if the image isn't inserting the function must have failed, so that might help us narrow it down.

0 Kudos
Message 4 of 15
(4,014 Views)

The error message reported by CA_GetAutomationErrorString is "Exception Occured", which is not very helpful.

 

Tony G.

0 Kudos
Message 5 of 15
(4,009 Views)

Tony,

 

What version of CVI are you using? I can see there was a bug in CVI 2009 SP1 related to the InsertImage command: http://www.ni.com/white-paper/12424/en#toc7 It looks like it was fixed in CVI 2010. 

 

Would it be possible to see the snippet of code that's inserting the image?

0 Kudos
Message 6 of 15
(4,002 Views)

I have CVI 9.0.0 (358).  I assume that's the same thing as CVI 2009?  If so, then it looks like you found the issue.  Hopefully I can use this as a justification to upgrade!  Thanks for your help.

 

Tony G.

0 Kudos
Message 7 of 15
(3,999 Views)

Hi Tony,

 

CVI 2009 is the version after CVI 9.0, see here

0 Kudos
Message 8 of 15
(3,996 Views)

I think I've found a problem with the wordreport instrument.  When I added error checking for all of the worderport and Word 9.0 Object... calls to my program, I found that the wordreport function WordRpt_SetReportLineSpacing returns an error.  I stepped into it and found that the first line in the function:

 

    errChk (Word_GetProperty (docHandle, NULL, Word_SelectionParagraphs,
                              CAVT_OBJHANDLE, &paragraphsHandleL));

returns error -2147352571 = "Type mismatch."  The function call looks lright to me, so I'm not sure what the problem is.

 

Tony G.

 

 

0 Kudos
Message 9 of 15
(3,993 Views)

Tony,

 

The problem might lie in the arguments you send to the SetReportLineSpacing function, since those are used by the GetProperty function that's throwing the error. Specifically, the document handle is used by the GetProperty function. Are you sure that the document handle you're passing in has been initialized as a handle to the Word document object, and that it hasn't been discarded with CA_DiscardObjHandle?

 

Also, the Line Spacing input can be one of three constants: WRConst_Single, WRConst_Double, or WRConst_15. So if you're using something other than one of those three options, that could cause the problem.

0 Kudos
Message 10 of 15
(3,987 Views)