Log in

View Full Version : [SOLVED:] Need macro to change table properties of two different indentation schemes



janaboo13
01-30-2015, 06:11 PM
I'm trying to create a macro(s) to go to each table (and there are lots of them) in a published document from a content management system, and then maybe change the left indent and/or the width. I can't use table styles unfortunately in my db...or at least haven't figured out how to do that!

Seems like an easy thing to do, and I've done part of that with this macro:



Option Explicit

Sub FixTableWidth()
Dim myObject As Table
For Each myObject In ActiveDocument.Tables
myObject.Rows.LeftIndent = InchesToPoints(0.04)
myObject.PreferredWidthType = wdPreferredWidthPoints
myObject.PreferredWidth = InchesToPoints(6)
Next
End Sub


Works like a charm, but it does it to all tables irrespective of original indentation and width.

I have tables that are indented 0.2 inches and I don't want to change the left indent but I might need to change the width. I also have tables that I need to adjust the left indent to 0.04 inches and don't want to change the width.

I can't figure out how to test all tables to see what the existing Left indent and width is before I adjust them or do nothing.

I really need help to figure out code to do this:

1. If the left indent is 0 and the width is >5.25 inches, then I want to indent those 0.04 inches and make the width 6 inches.

2. If the left indent is 0 and the width is <5.25 inches, I only want to indent those 0.04 inches and do nothing to the width.

3. If the left indent is 0.2 inches and the width is > 5.25 inches, then I'll only adjust the width to 5.85 inches.

4. If the left indent is 0.2 inches and the width is <5.25 inches, then I don't want to do anything to the table.

I thought I could do this by searching for styles, but my client has used only "cellbody" style for all content the tables. Then I thought I could search for styles in the paragraph preceding the table, then select the table and adjust. I've identified those typical styles, but I couldn't figure out the code to test to see if there was even a table following that paragraph. I'm way out of my league with this one.

Any help would be greatly appreciated! Jan

gmayor
01-31-2015, 01:49 AM
I would have preferred to have seen a document sample, but I suspect the following would be close


Sub FixTableWidth()
Dim myObject As Table
For Each myObject In ActiveDocument.Tables
Select Case myObject.PreferredWidth
Case Is >= InchesToPoints(5.25)
If myObject.Rows.LeftIndent = 0 Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
myObject.PreferredWidthType = wdPreferredWidthPoints
myObject.PreferredWidth = InchesToPoints(6)
ElseIf myObject.Rows.LeftIndent = InchesToPoints(0.02) Then
myObject.PreferredWidthType = wdPreferredWidthPoints
myObject.PreferredWidth = InchesToPoints(5.85)
End If
Case Is < InchesToPoints(5.25)
If myObject.Rows.LeftIndent = 0 Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
End If
End Select
Next
End Sub

fumei
01-31-2015, 07:08 AM
The following demonstrates how VBA can be written in different ways, but still do the same thing.

The only real difference between them is that my code does NOT action tables with widths exactly5.25. Graham's uses > OR equal to (and < OR equal to). It is a reasonable assumption, but you did not actually specify - you just stated > and <; not >= and <=. I think Graham is correct as I think you probably do mean for it to be actioned if the width is exactly 5.25, but shrug, I wanted to show the variability of doing the same things in VBA.

My code uses the crucial factor as the LeftIndent (Select Case myObject.Rows.LeftIndent). Graham's uses the crucial factor as PreferredWidth. It does not really matter as long as the LOGIC ends up with the same result.
Sub FixTableWidth()
Dim myObject As Table
For Each myObject In ActiveDocument.Tables
myObject.PreferredWidthType = wdPreferredWidthPoints
Select Case myObject.Rows.LeftIndent
Case 0
If myObject.PreferredWidth > InchesToPoints(5.25) Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
myObject.PreferredWidth = InchesToPoints(6)
ElseIf myObject.PreferredWidth < InchesToPoints(5.25)
myObject.Rows.LeftIndent = InchesToPoints(0.04)
End if
Case 0.2
If myObject.PreferredWidth > InchesToPoints(5.25) Then
myObject.PreferredWidth = InchesToPoints(5.85)
End If
End Select
Next
End Sub

janaboo13
01-31-2015, 12:58 PM
I would have preferred to have seen a document sample, but I suspect the following would be close


Sub FixTableWidth()
Dim myObject As Table
For Each myObject In ActiveDocument.Tables
Select Case myObject.PreferredWidth
Case Is >= InchesToPoints(5.25)
If myObject.Rows.LeftIndent = 0 Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
myObject.PreferredWidthType = wdPreferredWidthPoints
myObject.PreferredWidth = InchesToPoints(6)
ElseIf myObject.Rows.LeftIndent = InchesToPoints(0.02) Then
myObject.PreferredWidthType = wdPreferredWidthPoints
myObject.PreferredWidth = InchesToPoints(5.85)
End If
Case Is < InchesToPoints(5.25)
If myObject.Rows.LeftIndent = 0 Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
End If
End Select
Next
End Sub


Hi! Thank you so much for this. We're almost there. This works great except for the tables that are already indented. Turns out that the left indent of these tables (according to there properties) are 0.19 inches instead of 0.2 inches. I changed the ElseIf statement to reflect that, but the macro is still not fixing the width. Any ideas? Am I not expressing the InchestoPoints properly?
Thank you so much. Jan

janaboo13
01-31-2015, 01:03 PM
Hi fumei! Thank you for responding so quickly. I've tried this and for the most part it's working great on all the tables with 0 inch indent. However, it's not getting the tables that are already indented. The left indent (after checking the table properties) is 0.19 inches not 0.2 inches. I changed that in Case 0.2, but it still didn't change the width of these tables. Any suggestions on how to fix that?

Thank you so much! Jan

janaboo13
01-31-2015, 02:54 PM
Hi Graham!
After some further tweaking, I've gotten all the table that have 0 left indent to indent to 0.04 and be no wider than 6 inches. I'm still not getting the indented tables to change size to 5.85 inches and can't for the life of me figure that out. Here's the code I came up with:



Sub FixTableWidth3()
Dim myObject As Table
For Each myObject In ActiveDocument.Tables
Select Case myObject.PreferredWidth
Case Is >= InchesToPoints(6)
If myObject.Rows.LeftIndent = 0 Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
myObject.PreferredWidthType = wdPreferredWidthPoints
myObject.PreferredWidth = InchesToPoints(6)
ElseIf myObject.Rows.LeftIndent = InchesToPoints(0.19) Then
myObject.PreferredWidthType = wdPreferredWidthPoints
myObject.PreferredWidth = InchesToPoints(5.85)
End If
Case Is <= InchesToPoints(4)
If myObject.Rows.LeftIndent = 0 Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
End If
End Select
Next
End Sub


As I mentioned before, when I look at the table properties of an indented table, it's 0.19 inches from left so I changed the value to 0.19. The other thing that's happening is I have tables that are about 3 inches wide at left indent of 0 and when I run this, it indents them 0.04 AND makes them 6 inches wide. I thought by changing the PreferredWidth to >= 6 that it would ignore those. So it seems that the second Case is not working for some reason, right?

Thanks, jan

gmayor
01-31-2015, 11:29 PM
I think the problem relates to the setting of the preferred width option before checking for it:



Dim myObject As Table
For Each myObject In ActiveDocument.Tables
Select Case myObject.PreferredWidth

'change to

Dim myObject As Table
For Each myObject In ActiveDocument.Tables
.PreferredWidthType = wdPreferredWidthPoints
Select Case myObject.PreferredWidth
The preferred width is also being converted from points to inches so the measurement may not be exact. See what the following macro says about the table in question:


Sub Macro1()
With Selection.Tables(1)
.PreferredWidthType = wdPreferredWidthPoints
MsgBox "Preferred width - " & .PreferredWidth & vbCr & _
"Indent - " & .Rows.LeftIndent
End With
End Sub

janaboo13
02-01-2015, 10:03 AM
Hi Graham!
OK...tried the change to the macro and am getting a "Compile error - Invalid or unqualified reference" . . .I'm lost.

I also ran the other macro to determine the preferred width and indent against one of the tables that aren't working: Preferred Width - 9999999, Indent - 14.
So what does this tell us about the preferred width? Since we know what the left indent is in points, do I need to convert that to inches and specify that as the second Case statement?

Thank you so much for your help! Jan

gmayor
02-01-2015, 11:44 PM
Oops! Sorry about that :(.
It should of course have been


For Each myObject In ActiveDocument.Tables
myObject.PreferredWidthType = wdPreferredWidthPoints
Select Case myObject.PreferredWidth

janaboo13
02-16-2015, 12:18 PM
Hi All! Thanks for the help everyone. Here's the final code that I got to work:



Sub FixTableWidth()
Dim myObject As Table
For Each myObject In ActiveDocument.Tables
myObject.PreferredWidthType = wdPreferredWidthPoints
Select Case myObject.Rows.LeftIndent
Case InchesToPoints(0)
If myObject.PreferredWidth >= InchesToPoints(5.75) Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
myObject.PreferredWidth = InchesToPoints(6)
ElseIf myObject.PreferredWidth <= InchesToPoints(5.25) Then
myObject.Rows.LeftIndent = InchesToPoints(0.04)
End If
Case InchesToPoints(0.1944444444446)
If myObject.PreferredWidth >= InchesToPoints(6.03) Then
myObject.PreferredWidth = InchesToPoints(5.85)
End If
Case InchesToPoints(0.4027777777781)
If myObject.PreferredWidth >= InchesToPoints(6.03) Then
myObject.PreferredWidth = InchesToPoints(5.65)
End If
End Select
Next
End Sub


Jan