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:
HRESULT hr = CoInitialize(NULL);
_ApplicationPtr myApp("InDesign.Application");
DocumentPtr myDoc = myApp->ActiveDocument;
PagePtr myPage = myDoc->Pages->Item[1L];
TextFramePtr myTextFrame = myPage->TextFrames->Add(); TextFramePtr myTextFrame_2 = myPage->TextFrames->Item[2L];
myTextFrame have a GeometricBounds method by means of which the frame size changes. In a code below I set the frame size:
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:
_variant_t bounds = myTextFrame->GetGeometricBounds();
if ( (VT_ARRAY | VT_VARIANT) == bounds.vt)
{
SAFEARRAY *safe = bounds.parray;
if ( safe->cDims == 1 &&
safe->cbElements == sizeof (VARIANT) &&
(safe->rgsabound[0].cElements - safe->rgsabound[0].lLbound) == 4 )
{
VARIANT *values = (VARIANT *)safe->pvData;
double fetch[4] = {0};
for (int i = 0; i < 4; i++)
{
if (values[i].vt == VT_R8)
{
fetch[i] = values[i].dblVal;
}
}
}
}
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.
myTextFrame->FillColor = SomeSwatch->Name;
Got the collection:
SwatchesPtr mySwatches = myApp->Swatches;
Got Swatch instance and Swatch Name:
SwatchPtr mySwatch_1 = mySwatches->Item[1L];
_variant_t mySwatchName_1 = mySwatch->Name;
Apply swatch to frame:
myTextFrame->FillColor = mySwatchName_1;
When i get second swatch, get error:
double SwatchCount = mySwatches->Count; std::cout << SwatchCount << std::endl; 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?