Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Randomly applying font colour (VBA/Word/2003)

    Hi all,

    I have been asked to create a new template as part of my company's rebranding exercise. One of the features requested was that the template would have a palette of six colours that should randomly be applied to certain lines of text without user input. I have attached a simple document by way of demonstration.

    Let's say that the RGB values of the colour palette are as follows:

    Colour1 R:246 G:52 B:20
    Colour2 R:57 G:33 B:233
    Colour3 R:41 G:225 B:50
    Colour4 R:254 G:242 B:12
    Colour5 R:251 G:122 B:15
    Colour6 R:181 G:85 B:167

    Every time a user generates a new document, one of the above colours will be applied to the text at the following bookmarks:

    "title" (this bm is in the header)
    "positioning" (this bm is in the header)
    "name"
    "website" (this bm is in the footer)

    Can anyone help me get started please?

    Many thanks,

    Violet
    Attached Files Attached Files

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Randomly applying font colour (VBA/Word/2003)

    I do this in my UnderTheHood application. I can email a copy to you on request.
    It randomizes by characters, words and paragraphs.
    You can set the colors in a pallete.
    Attached Images Attached Images

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Randomly applying font colour (VBA/Word/2003)

    See the attached zipped template.
    Attached Files Attached Files

  4. #4
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Randomly applying font colour (VBA/Word/2003)

    Genius! This looks as if it will do exactly what we want, thanks so much.

    Violet

  5. #5
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Randomly applying font colour (VBA/Word/2003)

    Hi there,

    I've moved on slightly with this one but have hit another snag. Now that the colour randomisation is working (you'll see I had to go with styles rather than rgb values), I need to insert company logos that correspond with the style colour. I have inserted a bookmark (front_logo) in the header of the document template and simply want to insert the required logo (.jpg) at that position, however, when running the template, I receive the following error:

    "run-time error '13': type mismatch"

    I'm not sure what else to try - I've also tried simply cursoring (and even tabbing) to the position that the logo needs to be inserted at, all to no avail either.

    Thanks,

    Violet

    PS Why is it so difficult to work in the header of a template when using vb?
    Attached Files Attached Files

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Randomly applying font colour (VBA/Word/2003)

    Your problem is the code that says
    <code> If ActiveDocument.Styles("TITLE1") = True Then</code>
    A style doesn't have a True False value.

    You could possibly try
    <code> If ActiveDocument.Styles("TITLE1").InUse = True Then</code>
    but this seems to return true for all of your numbered styles.

    Probably the best approach for you will be
    <code>
    Select Case ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary).Range.Paragraphs(1).Style
    Case "TITLE1"
    <font color=448800>' code here for this title style</font color=448800>
    Case "TITLE2"
    <font color=448800>' code here for this title style</font color=448800>
    Case "TITLE3"
    <font color=448800>' code here for this title style</font color=448800>
    <font color=448800>' etc...</font color=448800>
    End Select
    </code>

    StuartR

  7. #7
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Randomly applying font colour (VBA/Word/2003)

    Thank you Stuart, that was spot on!

    Enjoy the rest of your day.

    Violet

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •