Results 1 to 11 of 11

Thread: Random Colours

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

    I have a word template (winXP, office2003) where i need to be able to achieve the insertion of six different colours of logo randomly. I have some templates that work on that basis already,

    Can anyone tell me why?

    Thanks,

    Violet
    Attached Files Attached Files

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,401
    Thanks
    208
    Thanked 833 Times in 766 Posts
    Violet,

    The template contains the following Auto_New VBA code:
    Code:
    Sub autonew()
        
        Dim intI As Integer
        Dim stylename As String
        'SET COLOURS
        Randomize
        intI = Int(Rnd * 6 + 1)
        
        stylename = "TITLE" & intI
        
        ActiveDocument.Bookmarks("hidden").Range.Style = stylename
        
        'CommandBars("Level Style Numbering").Visible = False
        'CommandBars("Text Style Numbering").Visible = False
        
        'ActiveWindow.View.TableGridlines = Not ActiveWindow.View.TableGridlines
    
    End Sub
    This code is run when the New Document event is fired, i.e. when you create a new document from the template.

    Note: Lines started with a ' are commented out and NOT executed.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. #3
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi RG, apologies if this reply appears twice - lost it a moment ago. I just changed the file extension of the upload to .doc from .dot as was unable to upload .dot. In the real world, I publish this to the organisation as a .dot, from which the users create new documents. I also know that the commented out text doesn't execute, thanks. My question, really, is why the randomisation process isn't happening and only one colour of logo keeps loading. Sorry if I've missed the point of your reply. Thanks again, V.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    If I add Debug.Print stylename after you have built the name, and then press F5 repeatedly, I get this in the Immediate Window:

    TITLE4
    TITLE5
    TITLE4
    TITLE3
    TITLE6
    TITLE3
    TITLE3
    TITLE6
    TITLE6
    TITLE2
    TITLE6
    TITLE6
    TITLE6
    TITLE2
    TITLE5
    TITLE6
    TITLE3
    TITLE2
    TITLE6
    TITLE1

    So I think it is randomizing, but if you prefer it to cycle through in a more orderly manner, perhaps you should use the clock instead? For example, to choose the color based on which "sixth" of the minute the second hand has reached:

    Code:
    intI = Round(((DatePart("s", Now()) + 1) / 10) + 0.4, 0)
    Or to cycle once per second:

    Code:
    intI = (DatePart("s", Now()) Mod 6) + 1
    Just a thought.

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,536
    Thanks
    3
    Thanked 147 Times in 140 Posts
    When the code runs it only changes the stylename applied to the first paragraph in the header (where the bookmark named 'hidden' is sitting). This changes the colour of the text in that empty paragraph but doesn't change a graphic. If you want to see this happen on screen then you need to put some text into the first paragraph of the header.

    The macro works as expected for me. The styles applied appear to be random enough but sometimes consecutive runs give the same style. This has the equivalent probability as rolling a pair with two dice {die} which is a 1 in 6 chance.

    I note that two pairs of the colours look pretty similar when viewed in the header so you might miss the subtle changes of colour between these pairs.
    Attached Images Attached Images
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Andrew,

    Thanks for your post. I had tried adding text at the "hidden" bookmark, but still the same colour logo (orange) always appeared. I did this by trying "ActiveDocument.Bookmarks("hidden").Range.Text = "hidden" in both the NewMacros module and the ThisDocument object. How were you going about adding text? The other templates I have, where the randomisation works, have userforms and I am wondering if that is the missing link. (There are two similar colours yes...that's design companies for you!)

    Thanks very much!

    Violet

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,536
    Thanks
    3
    Thanked 147 Times in 140 Posts
    Violet
    I just typed into the first line of the header.

    To test the macro:
    Type some text into that first line of the header
    Arrange your screen windows so you can see both the header and the VBA Editor.
    Now put your cursor in the macro and press F8 to step through the code.
    When the last line "ActiveDocument..." is highlighted, hover your mouse over the stylename variable to check its value
    Now press F8 again and see whether the style of the header was changed to match

    Is that not working?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Andrew,

    Yes it works in terms of chaging the style applied at the hidden bookmark (although I don't actually want any text to appear there), but the matching coloured logo (achieved by running the code in the "This Document" object) is what isn't working - it's always the orange logo that appears.

    Cheers,

    V

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,536
    Thanks
    3
    Thanked 147 Times in 140 Posts
    Why do you need two automacros to do one job? I would suspect that the macro in the "This Document" container is either not running or running before the other one changes the style. Stick a MsgBox in each of them to verify that both run and in which order.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yeah I've done that already with the msgboxes and they are both running, and the "ThisDocument" running after "NewMacros". Stumped.

  11. #11
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts
    CRACKED IT!!! The header had "different first page applied" and as soon as I noticed that and removed it, it all worked beautifully, just as it should. Happy days :-) Thanks for all the replies guys. Have a great 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
  •