11 Replies Latest reply on Jun 17, 2009 2:24 AM by goh6613

    fill color in textframe

    goh6613 Level 1

      hi all

       

      i try to fill color for a textframe

       

      for example :

       

      TextFrameTool.ParentStory.FillColor = MyDoc.Swatches.Item("C=15 M=100 Y=100 K=0"), work fine

       

      but

       

      strCMYK = "C=15 M=100 Y=100 K=0"

       

      TextFrameTool.ParentStory.FillColor = MyDoc.Swatches.Item(strCMYK ) , got error

       

      or

       

      dim sCMYK(3) as string

       

      sCMYK(0) = 15

      sCMYK(1) = 100

      sCMYK(2) = 100

      sCMYK(3) = 0

       

      TextFrameTool.ParentStory.FillColor = MyDoc.Swatches.Item(sCMYK ) , also got error

       

      may i know what is the problem....each time the color will be different, so i need to pass variable for different color...

       

      thanks

        • 1. Re: fill color in textframe
          goh6613 Level 1

          hi

           

          another question is how can i add color to MyDocument

           

          i suspect that the color is not in MyDocument so it return me error....

           

          can someone provide me the example of add color (vb.net)

           

          thanks

          • 2. Re: fill color in textframe
            Kasyan Servetsky Level 5

            1. This works for me (CS3, VB6).

             

            Dim myInDesign As InDesign.Application
            Dim myDoc As InDesign.Document
            Set myInDesign = CreateObject("InDesign.Application.CS3")
            Set myDoc = myInDesign.ActiveDocument
            Set TextFrameTool = myDoc.TextFrames.Item(1)
            strCMYK = "C=15 M=100 Y=100 K=0"
            TextFrameTool.ParentStory.FillColor = myDoc.Swatches.Item(strCMYK)

             

            You can't use arrays to reference swatches:

            Property Item(Index) As Object
                read-only
                Default member of InDesign.Swatches
                Returns the Swatch with the specified index or name. as Long Integer or String

             

            2. Put the line that sets color inside try block of try/catch construction. If error occurs then swatch doesn't exist, so add it and assign color inside catch block.

             

            Kasyan

            1 person found this helpful
            • 3. Re: fill color in textframe
              goh6613 Level 1

              hi

               

              my error here is i dont have the color in swatch (when i assign to it, it return me error),

               

              but how can i add new color to swatch ?

               

              thanks

              • 4. Re: fill color in textframe
                Dave Saunders Level 4

                Add a color to the document's collection of colors. It shows up as a swatch.

                 

                Dave

                1 person found this helpful
                • 5. Re: fill color in textframe
                  goh6613 Level 1

                  hi dave,

                   

                  may i know how to add color to the document? got any example ?

                   

                  i am using vb.net, i tried the code below but everytimes is add wrong color....

                   

                            Dim sCMYK(3) As String
                            Dim strCMYK As String
                            strCMYK = ""
                            sCMYK(0)=20

                            sCMYK(0)=20

                            sCMYK(0)=100

                            sCMYK(0)=0        

                            strCMYK = "C=" & sCMYK(0) & " M=" & sCMYK(1) & " Y=" & sCMYK(2) & " K=" & sCMYK(3)

                   

                            Dim sColor As InDesign.Color

                            sColor = MyDoc.Colors.Add
                            sColor.ColorValue = sCMYK
                            sColor.Name = strCMYK

                   

                  i not sure whether the 'ColorValue' can be set like this or not

                   

                  hopefully you can help me solve this problem

                   

                  thanks

                  • 6. Re: fill color in textframe
                    Kasyan Servetsky Level 5

                    Set myInDesign = CreateObject("InDesign.Application.CS3")
                    Set myDoc = myInDesign.ActiveDocument
                    Set TextFrameTool = myDoc.TextFrames.Item(1)
                    strCMYK = "C=15 M=100 Y=100 K=0"
                    On Error Resume Next
                    Set myColor = myDoc.Colors.Item(strCMYK)
                    If Err.Number <> 0 Then
                        Set myColor = myDoc.Colors.Add
                        myColor.Name = strCMYK
                        myColor.Model = idColorModel.idProcess
                        myColor.ColorValue = Array(15, 100, 100, 0)
                        Err.Clear
                    End If
                    On Error GoTo 0
                    TextFrameTool.ParentStory.FillColor = myColor

                    • 7. Re: fill color in textframe
                      goh6613 Level 1

                      hi

                       

                      i dont know why everytime it generate the same color even the array is different value

                       

                      i think the problem is at 'ColorValue' there, but i am not sure whether my code is correct to set its value ...

                       

                      can someone help me in this ?

                       

                      my code :

                       

                      Dim sCMYK(3) As String
                              Dim strCMYK As String

                              strCMYK = ""
                              sCMYK = ColorFormula_obj.GetCMYK(strColor)
                              strCMYK = "C=" & sCMYK(0) & " M=" & sCMYK(1) & " Y=" & sCMYK(2) & " K=" & sCMYK(3)

                              On Error Resume Next
                              Dim sColor As InDesign.Color

                              sColor = MyDoc.Colors.Item(strCMYK)

                              If Err.Number <> 0 Then

                                  sColor.ColorValue = sCMYK
                                  sColor.Model = idColorModel.idProcess
                                  sColor.Name = strCMYK
                                  sColor.Space = idColorSpace.idCMYK
                                  sColor = MyDoc.Colors.Add
                                  Err.Clear()
                              End If

                              On Error GoTo 0
                              TextFrameTool.ParentStory.FillColor = sColor
                              TextFrameTool.Contents = strCMYK
                            

                      thanks

                      • 8. Re: fill color in textframe
                        Kasyan Servetsky Level 5

                        I can't figure out what this line means:

                        sCMYK = ColorFormula_obj.GetCMYK(strColor)

                         

                        But it seems that I used a similar approach in one of my scripts:

                        http://kasyan.ho.com.ua/downloads/XML%20Import.zip

                         

                        function CheckSwatches() {
                            for (i=0; i<myColorsValues.length; i++)  {
                                var myCurArray = myColorsValues[i];
                                var myColorName = "C=" + myCurArray[0] + " M=" + myCurArray[1] +  " Y=" + myCurArray[2] + " K=" + myCurArray[3];
                                if (myDocument.swatches.item(myColorName) == null){
                                    var myCurColor = myDocument.colors.add({colorValue:myCurArray, name:myColorName});
                                    myColors.push(myCurColor);
                                }
                                else {
                                    myColors.push(myDocument.colors.item(myColorName));
                                }
                            }
                        }

                        • 9. Re: fill color in textframe
                          goh6613 Level 1

                          hi

                           

                          Dim sCMYK(3) As String

                          sCMYK = ColorFormula_obj.GetCMYK(strColor) will return me array

                           

                          it will be like

                           

                          sCMYK(0) = 20

                          sCMYK(1) = 50

                          sCMYK(2) = 80

                           

                          sCMYK(3) = 0

                           

                          but when i assign this array to 'ColorValue', it always return me wrong color (it add the color not according to the array that i assign to it)

                           

                          is it possible that my array declaration got problem ?

                           

                          thanks

                           

                           

                          • 10. Re: fill color in textframe
                            Kasyan Servetsky Level 5

                            You should have declared the array as Double, not as String.


                            Dim myInDesign As InDesign.Application
                            Dim myIdDoc As InDesign.Document
                            Dim sCMYK(3) As Double
                            Set myInDesign = CreateObject("InDesign.Application.CS3")
                            Set MyDoc = myInDesign.ActiveDocument
                            Set TextFrameTool = MyDoc.TextFrames.Item(1)
                            sCMYK(0) = 20
                            sCMYK(1) = 50
                            sCMYK(2) = 80
                            sCMYK(3) = 0
                            strCMYK = "C=" & sCMYK(0) & " M=" & sCMYK(1) & " Y=" & sCMYK(2) & " K=" & sCMYK(3)
                            On Error Resume Next
                            Set myColor = MyDoc.Colors.Item(strCMYK)
                            If Err.Number <> 0 Then
                                Set myColor = MyDoc.Colors.Add
                                myColor.Name = strCMYK
                                myColor.Model = idColorModel.idProcess
                                myColor.ColorValue = sCMYK
                                MsgBox (strCMYK & " — Color has been added")
                                Err.Clear
                            End If
                            On Error GoTo 0
                            TextFrameTool.ParentStory.FillColor = myColor

                            • 11. Re: fill color in textframe
                              goh6613 Level 1

                              thanks Kasyan Servetsky

                               

                              now the problem is solve after the change the declaration of the array