PDA

View Full Version : Solved: Call up color dialog box and select color



riduwanz
03-08-2005, 09:15 PM
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.


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


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. :bow:

Regards
Rid

Anne Troy
03-08-2005, 09:23 PM
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.

riduwanz
03-08-2005, 10:41 PM
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

Anne Troy
03-08-2005, 10:53 PM
you can see from the way I do the coding

That's hilarious. You think I read that stuff? Heck no!! :)

fumei
03-09-2005, 02:19 PM
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.

riduwanz
03-09-2005, 07:05 PM
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

riduwanz
03-11-2005, 03:59 AM
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.


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


Regards
Rid