![]() |
|
#31
|
||||
|
||||
|
As coded, the macro requires the heading row parameter to be present, to make it optional, it needs to be the last parameter: Parms: Col=3, RGB=215,199,244, Hdrs=0 The macro can then be recoded as: Code:
Sub TblHiLite()
Application.ScreenUpdating = False
Dim Hdr As Long, S As Long, W As Long, C As Long, R As Long, H As Long, i As Long
Dim StrTitle As String, StrParms As String, StrRGB As String
'Abort if the cursor is not in a table
If Selection.Information(wdWithInTable) = False Then
MsgBox "The selection is not in a table!", vbOKOnly, "TblHiLite"
GoTo ErrExit
End If
'Get Parameters
On Error GoTo ErrExit
StrParms = Split(Selection.Tables(1).Range.Previous.Paragraphs.Last.Range.Text, vbCr)(0)
For i = 0 To UBound(Split(StrParms, "="))
Select Case i
i = 0: C = Trim(Split(Split(StrParms, "=")(1), ",")(0))
i = 1: StrRGB = Trim(Split(Split(StrParms, "=")(2), ",")(0))
i = 2: Hdr = Trim(Split(StrParms, "=")(3))
End Select
Next
S = RGB(Split(StrRGB, ",")(0), Split(StrRGB, ",")(1), Split(StrRGB, ",")(2))
On Error GoTo 0
W = RGB(255, 255, 255)
'process the table
With Selection.Tables(1)
'Validate the column count
If C > .Columns.Count Then
MsgBox "There is no column " & C & " in the table!", vbOKOnly, "TblHiLite"
End
End If
'Validate the heading row count
If Hdr = 0 Then
For i = 1 To .Rows.Count
If .Rows(i).HeadingFormat = True Then
Hdr = Hdr + 1
Else
Exit For
End If
Next
ElseIf Hdr > .Rows.Count Then
MsgBox "There is no row " & Hdr & " in the table!", vbOKOnly, "TblHiLite"
End
End If
StrTitle = Split(.Cell(1 + Hdr, C).Range.Text, vbCr)(0)
.Rows(1 + Hdr).Shading.BackgroundPatternColor = W: H = W
For R = 2 + Hdr To .Rows.Count
If Split(.Cell(R, C).Range.Text, vbCr)(0) <> StrTitle Then
If H = W Then H = S Else H = W
StrTitle = Split(.Cell(R, C).Range.Text, vbCr)(0)
End If
.Rows(R).Shading.BackgroundPatternColor = H
Next
End With
Application.ScreenUpdating = True
End
ErrExit:
MsgBox "Invalid Parameter", vbExclamation
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#32
|
||||
|
||||
|
Quote:
Parms: 3, 215, 199, 244, 0 And in my code, all of them have defaults, so none of them are required. I loop through them one by one using a Case statement to determine which keyword it is and then process the value. I have actually come up with a couple more parameters as well, all of which can be coded in any order. Code:
ParmStr = Trim(Selection.Tables(1).Range.Previous.Paragraphs.last.Range.Text)
ParmStr = Left(ParmStr, Len(ParmStr) - 1)
Tokens = Split(ParmStr, " ")
If UBound(Tokens) < 1 Then GoTo NoInDocParms
If UCase(Tokens(0)) <> ParmKW Then GoTo NoInDocParms
For i = 1 To UBound(Tokens)
If Tokens(i) = "" Then
GoTo Continue_i: End If
KwVal = Split(UCase(Tokens(i)), "=")
If UBound(KwVal) <> 1 Then
msg = "Invalid keyword parameter (" & Tokens(i) & ")"
MsgBox msg, vbOKOnly, MyName: GoTo ErrExit: End If
Select Case Trim(KwVal(0))
Case "COLUMN"
... process the Column value
Case "Headers" 'If it's a Headers parameter,
... process the Headers value
Case "RGB1"
... process the RGB1 values
Case "RGB2"
... process the RGB2 values
Case "MATCHCASE"
... process the MatchCase value
Case Else
msg = "Invalid keyword parameter (" & Tokens(i) & ")"
MsgBox msg, vbOKOnly, MyName: GoTo ErrExit
End Select
Continue_i:
Next i
Quote:
But I've never seen a Case statement coded like that. It's always: Code:
Select Case i
Case 0: C = Trim(Split(Split(StrParms, "=")(1), ",")(0))
Case 1: StrRGB = Trim(Split(Split(StrParms, "=")(2), ",")(0))
Case 2: Hdr = Trim(Split(StrParms, "=")(3))
End Select
|
|
#33
|
||||
|
||||
|
The code I supplied in post #21.
Quote:
and requires each parameter to be preceded by '=' and, except for the last, to be followed by ','. The order in which they occur is critical - regardless of whether you supply the keywords (which aren't actually necessary for the parsing). For example, the code cannot possibly know which is the column and the row if you use: 3, 1, 215, 199, 244 or: 1, 3, 215, 199, 244 unless you stick to a specified order. Yes, you could just pass a string of numbers separated by whatever character you choose (e.g. just a space or a comma) but, even so, the order would remain critical. Quote:
Quote:
which means one parameter has become optional and, so, a way is needed to test for its presence.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#34
|
||||
|
||||
|
Quote:
Once you showed me how to have a parameter string in the document, it was always my intention to have keyword parameters that could be coded in any order and for all of them to have default values in case no parameters at all are passed. The general syntax is something like this: Code:
TblHiLite: [Column=C|1] [Headers=H|1] [RGB1=R,G,B|255,255,255]
[RGB2=R,G,B|219,229,241] [MatchCase=Yes/No|Yes]
The order doesn't matter and none of them are required. This is a very flexible syntax and easy for the user (me) to use.
|
|
#35
|
||||
|
||||
|
I just realized what this statement of your really does:
Code:
Split(.Cell(Row, Column).Range.Text, vbCr)(0) Code:
.Cell(Row, Column).Range.Text In any case, your code neatly captures just the text. I assume that there is no way to retrieve just the (visible) text, right? |
|
#36
|
||||
|
||||
|
Table cells and rows always end with a Chr(13) & Chr(07) pair. At a guess, I'd say the Chr(07) is a replacement for the Chr(10) that is paired with Chr(13) to form a normal paragraph break.
No, the code I used shows one way. Do be aware that, even with an ordinary paragraph, .Range.Text includes the paragraph break. In a multi-paragraph cell, or if you're wanting to capture the text and its formatting, different code would be required to capture all paragraphs and omit the end-of-cell marker (i.e. the Chr(13) & Chr(07) pair).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Macro to highlight a list of words
|
bakerkr | Word VBA | 4 | 10-19-2017 02:23 PM |
Word 2010 VBA Print Macro - Specified Sections
|
Benbon | Word VBA | 3 | 03-30-2017 02:31 PM |
Macro Question: Need help making a macro to highlight the first word in every sentence
|
LadyAna | Word | 1 | 12-06-2014 10:39 PM |
Macro to highlight words
|
bertietheblue | Word VBA | 9 | 07-01-2013 12:39 PM |
| find - reading highlight - highlight all / highlight doesn't stick when saved | bobk544 | Word | 3 | 04-15-2009 03:31 PM |