apply changes to tables the user specifies by index number

setup

now that the user has the means of knowing the index numbers of the tables to be processed (see more in here), and i've got the means to know them, too (see more in here), it's time to apply some batch operation to them tables.

solution

the plan

the user finds out the index numbers of the tables to apply changes to, using vTblIdx or other tools.

  1. call fGetUserInpIdx.
  2. iterate the codes that effect the desired changes over the collection returned by fGetUserInpIdx.

programming language & module(s)

  • VBA

file preps

make the doc to be processed the active document, i.e. the one where the cursor currently is in.

variables to customize

none

the script

fmtTblDef
Sub fmtTblDef()
Dim doc As Document
Dim tblIdxColl As Collection
Dim tbl As Table
Set doc = ActiveDocument
Set tblIdxColl = fGetUserInpIdx()
For Each idx In tblIdxColl
Set tbl = doc.Tables(idx)
operateTblSub tbl
Next
End Sub
Function fGetUserInpIdx() As Collection
Dim rtn As Collection
Dim ret As Object
Dim prompt, inputErrPrompt As String
Dim startIdx, endIdx As Long
prompt = "please type in the index numbers. seperate by space (i.e. ' ')." & vbCr & _
"'1 22 3-5' for nos. 1, 3, 4, 5, and 22"
inp = InputBox(prompt)
If Len(Trim(inp)) > 0 Then
Set ret = CreateObject("VBScript.RegExp")
ret.pattern = "[^\d- ]"
If ret.test(inp) Then
inputErrPrompt = "it appears something other than numbers, spaces, and '-' were typed in." & vbCr & _
"please run the macro again."
MsgBox inputErrPrompt
Else
splitted = Split(inp, " ")
Set rtn = New Collection
ret.pattern = "(\d+) *- *(\d+)"
For i = LBound(splitted) To UBound(splitted)
itm = Trim(splitted(i))
If ret.test(itm) Then
startIdx = ret.Execute(itm)(0).submatches(0)
endIdx = ret.Execute(itm)(0).submatches(1)
For n = startIdx To endIdx
rtn.Add n
Next
Else
rtn.Add CInt(itm)
End If
Next
End If
End If
Set fGetUserInpIdx = rtn
End Function
Sub operateTblSub(ByVal tbl As Table)
tbl.Range.Font.Name = "Times New Roman"
tbl.Range.Font.Bold = True
End Sub

output

in the following example, the iterated operation is done by operateTblSub. it sets the fonts of all texts as Times New Roman and bolds them. image.png

note to self

none

Some rights reserved
Except where otherwise noted, content on this page is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.