Click here to Skip to main content
15,888,104 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a program with which I work through the directive #import. In it I create the new document and for the first page I place TextFrame which is created with parameters by default.
I can set value:
C++
HRESULT hr = CoInitialize(NULL);
_ApplicationPtr myApp("InDesign.Application");

DocumentPtr myDoc = myApp->ActiveDocument;
PagePtr myPage = myDoc->Pages->Item[1L];
TextFramePtr myTextFrame = myPage->TextFrames->Add(); // create frame on Page
TextFramePtr myTextFrame_2 = myPage->TextFrames->Item[2L]; // Get second frame on Page;


myTextFrame have a GeometricBounds method by means of which the frame size changes. In a code below I set the frame size:
C++
double mySize[4] = {12.7, 12.7, 66.7, 83.2};
SAFEARRAY * Bound;
VARIANT Array;
Array.vt =   VT_ARRAY | VT_R8;
Bound = SafeArrayCreateVector(VT_R8, 1, 4); 
for (int i = 0; i < 4; i++)
{
    long index = i + 1;
    SafeArrayPutElement(Bound, &index, &mySize[i]); 
}
Array.parray = Bound; 
myTextFrame->GeometricBounds = Array;


Get TextFrame GeometricBounds:
C++
_variant_t bounds = myTextFrame->GetGeometricBounds();
if ( (VT_ARRAY | VT_VARIANT) == bounds.vt)
{
    SAFEARRAY *safe = bounds.parray;
    // one dimension?
    if ( safe->cDims == 1 &&
        // correct element size?
        safe->cbElements == sizeof (VARIANT) &&
        // exactly four elements?
        (safe->rgsabound[0].cElements - safe->rgsabound[0].lLbound) == 4 )
    {
        VARIANT *values = (VARIANT *)safe->pvData;
        // assuming four variants - each with VT_R8.
        double fetch[4] = {0};
        for (int i = 0; i < 4; i++)
        {
            if (values[i].vt == VT_R8)
            {
                fetch[i] = values[i].dblVal;
            } 
        }
        // now fetch[0:3] has your values.
    }
}
else
    std::cout << "unexpected type." << std::endl;

Now I want to paint frame with one of predefined swatches. I have to give instance of Swatch to frame by FillColor, i.e.
C++
myTextFrame->FillColor = SomeSwatch->Name;

Got the collection:
C++
SwatchesPtr mySwatches = myApp->Swatches;

Got Swatch instance and Swatch Name:
C++
SwatchPtr mySwatch_1 = mySwatches->Item[1L]; 
_variant_t mySwatchName_1 = mySwatch->Name;

Apply swatch to frame:
C++
myTextFrame->FillColor = mySwatchName_1;

When i get second swatch, get error:
C++
double SwatchCount = mySwatches->Count; //get quantity of elements
std::cout << SwatchCount << std::endl; // SwatchCount = 10
SwatchPtr mySwatch_2 = mySwatches->Item[2L];
_variant_t mySwatchName_2 = mySwatch_2->Name;

What’s wrong with my code? why it isn't available from second Swatch to Swatch.Count?
Posted
Updated 17-Aug-15 21:37pm
v13
Comments
Sergey Alexandrovich Kryukov 10-Aug-15 14:10pm    
What do you mean by that? This is already a value. The type has some members which also have values. What's the problem?
—SA
Maciej Los 10-Aug-15 15:45pm    
Understand, but not notified.
Use "Reply" widget if you wish to notify member about your comment.
Nindzzya 10-Aug-15 15:51pm    
thanks)
Maciej Los 10-Aug-15 15:52pm    
You're very welcome ;)
[no name] 17-Aug-15 17:06pm    
@nindzzya - the Item array is zero based. Item[1] and Item[2] may be how VB sees it but not so for C++. Use Item[0] and Item[1] for two element array.

1 solution

GeometricBounds is a variant - which could be pretty much anything.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms221627%28v=vs.85%29.aspx[^]

The safe thing is to get the variant and check for an expected type (like a SAFEARRAY of four doubles).

https://msdn.microsoft.com/en-us/library/windows/desktop/ms221482%28v=vs.85%29.aspx[^]

Read this about SAFEARRAYs.

http://roblocher.com/whitepapers/oletypes.html[^]

C++
TextFramePtr myTextFrame = myPage->TextFrames->Add(); // create frame on Page

_variant_t bounds = myTextFrame->GetGeometricBounds();
if ( (VT_ARRAY | VT_VARIANT) == bounds.vt)
{
    SAFEARRAY *safe = bounds.parray;
    // one dimension?
    if ( safe->cDims == 1 &&
        // correct element size?
        safe->cbElements == sizeof (VARIANT) &&
        // exactly four elements?
        (safe->rgsabound[0].cElements - safe->rgsabound[0].lLbound) == 4 )
    {
        VARIANT *values = (VARIANT *)safe->pvData;
        // assuming four variants - each with VT_R8.
        double fetch[4] = {0};
        for (int i = 0; i < 4; i++)
        {
            if (values[i].vt == VT_R8)
            {
                fetch[i] = values[i].dblVal;
            } 
        }
        // now fetch[0:3] has your values.
    }
}
else
    std::cout << "unexpected type." << std::endl;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900