Consulting

Results 1 to 7 of 7

Thread: Solved: Call up color dialog box and select color

  1. #1
    VBAX Regular
    Joined
    Feb 2005
    Posts
    10
    Location

    Solved: Call up color dialog box and select color

    Hello again,

    I have this section of codes that I pieced together with a lot of trials and errors. The purpose is to shade alternate rows of selected table.

    [VBA]
    lastRow = Selection.Information(wdMaximumNumberOfRows)
    If lastRow <= 3 Then
    MsgBox "No need for shading"
    Exit Sub
    End If
    For i = 3 To lastRow
    Selection.Tables(1).Rows(i).Select

    Selection.Cells.Shading.BackgroundPatternColor = wdColorLime
    i = i + 1
    Next i
    [/VBA]

    I would like to take it further by allowing the user to select the Shading color, and Pattern Style and color from Word color dialog box. The alternate rows are then shaded (or filled with pattern style and color) with the selected ones.

    Really appreaciate all the helps.

    Regards
    Rid

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Rid: You're always asking questions that intrigue me. I looked around, but found nothing. There are plenty of Excel macros to fill every other row, but I don't see anything for Word, which doesn't mean they don't exist, LOL.

    But for some of the Excel-type coders who might want to take a crack at this, I did find some references to code that works with tables: http://word.mvps.org/FAQs/MacrosVBA.htm#Tables

    I don't find that site very helpful, but others do. Maybe that's 'cause others know the difference between a property and a method. LOL. Anyway, I've chimed in so I can see how this progresses for you. I'd love to see it turn into a KB entry.
    ~Anne Troy

  3. #3
    VBAX Regular
    Joined
    Feb 2005
    Posts
    10
    Location
    Thanks Dreamboat,

    I have searched the Net for almost two days and I found nothing suitable (and being a newbie does not help :-), you can see from the way I do the coding). I went to Word MVPS site, but like you said, it did not exactly give me enough clues. I will continue my search and will come back for/with any update.

    Regards
    Rid

  4. #4
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    you can see from the way I do the coding
    That's hilarious. You think I read that stuff? Heck no!!
    ~Anne Troy

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Uh...what version are you using? As of Word XP (2002), you can create Table styles, and they can be formatted in just about any manner you choose. Alternating shades, colors, fonts...whatever you want. You can even make a created table style the default so clicking the table icon on the toolbar will make a table from that style.

    That way, click on a table...any table...select the style...done.

  6. #6
    VBAX Regular
    Joined
    Feb 2005
    Posts
    10
    Location
    Thanks Gerry,

    I am using Word 2003.

    I totally agree with you regarding the Table style part, but I have slight problem from clients' point of views...ie different clients want different colors/styles. And I assume it is easier to give the control to the DTP who understand the client requirements better (no???). I used the Table Style method about 9 months ago and like you said, assigned it to a button/shortcut. And I got feedback from the users that they are not using the toolbar at all.

    Regards
    Rid

  7. #7
    VBAX Regular
    Joined
    Feb 2005
    Posts
    10
    Location
    With Hans' wonderful codes and tips, I used the following codes to shade alternate rows in my tables. Anyone that can make the codes even simpler, please do not hesitate to correct them.

    [VBA]
    Sub AlternateRowShading()
    Dim dlgshading As Dialog
    Dim lastRow As Long
    Dim i As Integer
    Dim selectedTextureStyle As Long
    Dim selectedTextureBackgroundColor As Long
    Dim selectedTextureForegroundColor As Long
    If Selection.Information(wdWithInTable) = False Then
    MsgBox "Please position the insertion point in a table.", vbExclamation
    Exit Sub
    End If
    sampleRow = Selection.Tables(1).Rows(3)
    sampleRow.Select
    Set dlgshading = Dialogs(wdDialogFormatBordersAndShading)
    With dlgshading
    .DefaultTab = wdDialogFormatBordersAndShadingTabShading
    .Show
    End With
    selectedTextureStyle = sampleRow.Shading.Texture
    selectedTextureBackgroundColor = sampleRow.Shading.BackgroundPatternColor
    selectedTextureForegroundColor = sampleRow.Shading.ForegroundPatternColor
    lastRow = Selection.Information(wdMaximumNumberOfRows)
    For i = 5 To lastRow Step 2
    Selection.Tables(1).Rows(i).Select
    With Selection.Tables(1).Rows(i).Shading
    .Texture = selectedTextureStyle
    .BackgroundPatternColor = selectedTextureBackgroundColor
    .ForegroundPatternColor = selectedTextureForegroundColor
    End With
    Next i
    End Sub
    [/VBA]

    Regards
    Rid

Posting Permissions

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