• Not Answered

Problem for the VBA experts

If I have a paragraph text object tPara I can see the contents of the second paragraph as a textrange object at tPara.Text.Story.Paragraphs(2)

Consequently, I can take the contents of that paragraph :

dim t as textrange
set t = tPara.Text.Story.Paragraphs(2)

If I look in the debugger, I can then see that the t.Characters collection includes not only the characters themselves, but also the information associated with each character -- including fonts, attributes and font colours.

So, if I have an empty paragraph tPara2 -- why is it that a further assignment

tPara2.Text.Story.Paragraphs(1) = t

causes the paragraph text to receive only the plain text and not the associated attributes ? If the assignment is a textrange, surely the entire set of textrange objects should now be visible in tPara2, rather than just the plain text ?

[textAttributes.cdr attached for anybody who wants to check it out, the objects are referred to as sSource and sDest in the Button 1 code]



[edited by: harryLondon at 7:37 (GMT -5) on Mon, Nov 18 2013]
textAttributes.cdr

7 Replies - Latest Reply

  • Hi.

    I think it's a Draw limitation. You cannot have multiple attributes in the text string and copy them over. You would have to get the property for each character that has different properties aside from the standard single property that can be copied. Hopefully I'm wrong though.

    ~John

  • In reply to Sub GDG_John( ):

    It seems a bit lopsided ... if you can create a textrange object from a paragraph (and include the various attributes within it) then you also ought to have a way to create a paragraph from a textrange.

    The aim in this case is actually to exchange two paragraphs (complete with their various attributes) -- which probably ought to be as simple as swapping the pointers of the corresponding two elements. But I'm not sure if that's possible in VBA.



    [edited by: harryLondon at 10:37 (GMT -5) on Mon, Nov 18 2013]
  • In reply to harryLondon:

    Hi.

    Open your locals window. Place a break point after you set the textrange. Run code. Then when it breaks look in your locals window and search through the properties. Try to find where there are separate properties for the parts or individual characters. I don't think there are.

    Also you might try the method:

    TextRange.CopyAttributes

    Sub CopyAttributes(SourceRange As TextRange)

    Description

    Member of TextRange

    The CopyAttributes method copies the text attributes from the specified text range and applies them to the current text range.

    ~John

  • In reply to Sub GDG_John( ):

    CopyAttributes sounds quite promising, assuming that attributes means the font characteristics, colour, bold etc.

    I'm guessing that current text range has to be set with setRange. But adding

    sDest.Text.Story.Paragraphs(1).SetRange 1, sDest.Text.Story.Paragraphs(1).Length
    sDest.Text.Story.Paragraphs(1).CopyAttributes sSource.Text.Story.Paragraphs(pNumber)

    is not changing any attributes in the destination.

    I'm starting to wonder whether attributes means something else in this case.



    [edited by: harryLondon at 11:53 (GMT -5) on Mon, Nov 18 2013]
  • In reply to harryLondon:

    Hi.

    Test it on a basic piece of artistic text. Just bold one letter or something.

    I've never dug deep into this but never found the attributes to be able to be transferred so simply. For example if you change a font in part of the text string, this sub property or characteristic is not to be found.

    ~John

  • In reply to Sub GDG_John( ):

    Hi.

    Also have you tried copy and pasting with code? If copy and paste will work manually then it should  work with code.

    ~John

  • In reply to harryLondon:

    Harry, 

    The CopyAttributes should work, but you will need to do this for each character. This can be really slow, but here is an example:

        Dim trSource As TextRange
        Dim trDestination As TextRange
    
        Set trSource = ActiveSelectionRange.Shapes(1).Text.Story.Paragraphs(1)
        Set trDestination = ActiveSelectionRange.Shapes(2).Text.Story.Paragraphs(1)
        
        For i = 1 To trSource.End
            trDestination.Range(i, i).CopyAttributes trSource.Range(i, i)
        Next i
    

    I don't recommend copy and paste often, but in this case it is much faster. Here is an example of how you can use copy and paste:

        Dim tSource As CorelDRAW.Text
        Dim tDestination As CorelDRAW.Text
        
        Set tSource = ActiveSelectionRange.Shapes(1).Text
        Set tDestination = ActiveSelectionRange.Shapes(2).Text
        
        tSource.Story.Paragraphs(1).Copy
        tDestination.BeginEdit
        ActiveLayer.Paste
    

    -Shelby