PDA

View Full Version : Need help with Word vba loops



janaboo13
02-14-2015, 02:21 PM
Greetings experts!

As much as I hate to admit it, creating loops in Word vba gives me fits! I'm trying to search for a style (that happens to be in tables only), then select the table and adjust the table properties. After I'm done, I'd like the macro to find the next one and so on and so forth. I've searched for similar macros and can't find one that works for me.

Sure would appreciate any help you can give me!

Here's the recorded macro:



Sub Macro1()
'
' Macro1 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("CustomTableWidth")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute
Selection.Tables(1).Select
Selection.Tables(1).PreferredWidthType = wdPreferredWidthPoints
Selection.Tables(1).PreferredWidth = InchesToPoints(3.75)
End Sub


Jan

gmayor
02-14-2015, 11:16 PM
Loops are straightforward enough :)

See the following


Sub Macro1()
Dim oTable As Table
Dim oStyle As Style
Dim oCell As Cell

'Check each table
For Each oTable In ActiveDocument.Tables
'Check each cell in the current table
For Each oCell In oTable.Range.Cells
'Record the the cell style
Set oStyle = oCell.Range.Style
'If it is the named style
If oStyle.NameLocal = "CustomTableWidth" Then
'set the values you want to the current table
oTable.PreferredWidthType = wdPreferredWidthPoints
oTable.PreferredWidth = InchesToPoints(3.75)
End If
Next oCell
Next oTable
lbl_Exit:
'Clean up
Set oTable = Nothing
Set oCell = Nothing
Set oStyle = Nothing
Exit Sub
End Sub

gmaxey
02-15-2015, 07:39 AM
Graham is right. Typically loops are easy enough. Here is an alternative to the For Each ... Next loop that Graham provided. In my experience working with a large collection (e.g., say a table with 5,000 cells), it is considerably faster than For Each. I'm sure it was an oversight on Graham's part, once you find your style, you can get out of the loop.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRngTable As Word.Range, oRngCell As Word.Range
Set oRngTable = ActiveDocument.Tables(1).Range
Do
Set oRngCell = oRngTable.Cells(1).Range
Do
If oRngCell.Cells(1).Range.Style.NameLocal = "CustomTableWidth" Then
With oRngTable.Tables(1)
.PreferredWidthType = wdPreferredWidthPoints
.PreferredWidth = InchesToPoints(3.75)
End With
Exit Do
End If
Set oRngCell = oRngCell.Next(wdCell)
Loop Until oRngCell Is Nothing
Set oRngTable = oRngTable.Next(wdTable)
Loop Until oRngTable Is Nothing
End Sub

Back to your original approach:


Sub ScratchMacroII()
'A basic Word macro coded by Greg Maxey
Dim oRngTable As Word.Range
Set oRngTable = ActiveDocument.Tables(1).Range
Do
With oRngTable.Find
.Style = "CustomTableStyle"
If .Execute Then
With oRngTable.Tables(1)
.PreferredWidthType = wdPreferredWidthPoints
.PreferredWidth = InchesToPoints(3.75)
End With
End If
End With
Set oRngTable = oRngTable.Next(wdTable)
Loop Until oRngTable Is Nothing
End Sub

janaboo13
02-15-2015, 12:04 PM
Loops are straightforward enough :)

See the following


Sub Macro1()
Dim oTable As Table
Dim oStyle As Style
Dim oCell As Cell

'Check each table
For Each oTable In ActiveDocument.Tables
'Check each cell in the current table
For Each oCell In oTable.Range.Cells
'Record the the cell style
Set oStyle = oCell.Range.Style
'If it is the named style
If oStyle.NameLocal = "CustomTableWidth" Then
'set the values you want to the current table
oTable.PreferredWidthType = wdPreferredWidthPoints
oTable.PreferredWidth = InchesToPoints(3.75)
End If
Next oCell
Next oTable
lbl_Exit:
'Clean up
Set oTable = Nothing
Set oCell = Nothing
Set oStyle = Nothing
Exit Sub
End Sub



Thank you, Graham! However, I get this error: Object variable or With block variable not set.

Jan

janaboo13
02-15-2015, 12:06 PM
Greg! Your second solution working like a charm! Thanks so much. I was so close to making this work on my own (something similar to your solution), but didn't quite get there. I so appreciate the solution and can learn from this.

The first solution returned this error: Object variable or With block variable not set.

Can you explain what that really means???

Thanks again, Jan

gmaxey
02-15-2015, 05:48 PM
janaboo,

Neither my first suggestion or Graham's code is returning an error here with a simple Word document containing uniform tables. I would have to see you document to determine why you are getting the error.