FirstLineIndent read only in table cell

B

Ben Bullock

I am using Microsoft Word 2003 and trying to write a macro which
corrects some indenting problems in the cells of a document. I have a
problem in that I do not seem to be able to set the value of
FirstLineIndent, the indentation.

Here is a macro I've written to try to demonstrate the problem I'm
encountering:

Sub DoAllIndents()
Dim NowIndent As Long
For Each Tbl In ActiveDocument.Tables
For Each Row In Tbl.Rows
For Each cellLoop In Row.Cells
With cellLoop.Range
MsgBox (.Text)
NowIndent = .Paragraphs(1).FirstLineIndent
MsgBox (NowIndent)
.Paragraphs(1).FirstLineIndent = 0
MsgBox (.Paragraphs(1).FirstLineIndent)
End With
Next cellLoop
Next Row
Next Tbl
End Sub

The problem is that setting "FirstLineIndent" does nothing: the second
MsgBox gives the same value as the first one. Apart from using
Range.Paragraphs(1), I've also used .ParagraphFormat.FirstLineIndent
with exactly the same results. The document itself is not read-only,
but "FirstLineIndent" seems to be.

I've googled for some hints but have not been able to make the above
work properly.

Thank you very much indeed for any help with this problem.
 
M

macropod

Hi Ben,

Did you take a look at the table to see what actually happens? The first line indents are actually being set to 0. Obviously, if
they were 0 beforehand, you're not going to see any changes. If you're wanting to change the indent to a non-zero value, then you'll
need to indicate both the value and the unit of measure, for example for a 1/4" indent: FirstLineIndent = InchesToPoints(0.25).

You could also simplify your code, as shown below:

Sub DoAllIndents()
Dim oTbl As Table
Dim oCell As Cell
For Each oTbl In ActiveDocument.Tables
For Each oCell In oTbl.Range.Cells
oCell.Range.Paragraphs(1).FirstLineIndent = InchesToPoints(0.25)
Next oCell
Next oTbl
End Sub

Cheers
 
D

Doug Robbins - Word MVP

That code certainly changes (removes) the first line indent from the first
paragraph of each cell of a table here. Are you sure that there is not an
empty paragraph in the cell before the one that you are trying to reformat?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

Ben Bullock

Hi Ben,

Did you take a look at the table to see what actually happens? The first line indents are actually being set to 0. Obviously, if
they were 0 beforehand, you're not going to see any changes.

Yes, of course I have looked at them. The values don't change, they
are 20 before and 20 after the line.
 
B

Ben Bullock

That code certainly changes (removes) the first line indent from the first
paragraph of each cell of a table here.

Well, it doesn't. I tried recording a macro and it also doesn't work.
Another funny thing is that the "Decrease Indent" button also refuses
to work, so it is not just a macro problem. However, the slider at
the top of the page "First Line Indent" does alter the indentation.
Are you sure that there is not an
empty paragraph in the cell before the one that you are trying to reformat?

I'm absolutely sure. I'll post some screenshots tomorrow.

By the way, I worked out that if I clear the formatting I can remove
the First Line Indent as well.
 
D

Doug Robbins - Word MVP

Is there a Style applied to the paragraphs?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

macropod

Hi Ben,

Your code works for me, regardless of what formatting (including Style formatting) the paragraphs had beforehand. Maybe there's some
corruption in your document. Try copying the errant table out to another document and running the code on it there. If that works,
go back to the original document and copy all except for the last paragraph mark and paste the contents into a new document. Then
try your code again in the new document.

Cheers
 
B

Ben Bullock

Is there a Style applied to the paragraphs?

There seems to be a style on these paragraphs.

In the end I rewrote my program to clear the formatting and then
restore it (this is Perl but it uses the same commands as VBA):

# Clean up one cell of the table.

sub cleanup_cell
{
my ($cell, $x, $y) = @_;
# $indent is the visual indentation, which consists of first line
# indentation, left indentation, and spaces in some cases.
my $indent = $cell->Range->Paragraphs(1)->FirstLineIndent;
my $leftindent = $cell->Range->Paragraphs(1)->LeftIndent;
$indent += $leftindent;
my ($text, $spaces) = XYZ::normalize_text($cell->Range->Text, $x);
$indent += $spaces * 10;
# Fix all wonky double indents.
if ($x == 2) {
$indent=23 if $indent>=20;
} else {
$indent=20 if $indent>=20;
}
my $para = $cell->Range->Paragraphs(1);
# Replace the text before fixing the indentation, otherwise
# putting the text in buggers the indentation up again.
$cell->Range->{Text} = $text;
# Now change indenting
if ($indent > 0) {
$cell->Select();
$word->Selection->ClearFormatting();
$para->{LeftIndent} = $indent;
if ($x == 2) {
$word->Selection->Font->{Name} = "Times New Roman";
$word->Selection->Font->{Size} = 12;
}
}
# Remove justification.
$para->{Alignment} = wdAlignParagraphLeft;
}

For the time being I've solved the problem with this, but if I have
time I'll try to investigate what could have happened.
 
B

Ben Bullock

Your code works for me, regardless of what formatting (including Style formatting) the paragraphs had beforehand. Maybe there's some
corruption in your document. Try copying the errant table out to another document and running the code on it there. If that works,
go back to the original document and copy all except for the last paragraph mark and paste the contents into a new document. Then
try your code again in the new document.

Thank you, I will try that. For the time being I found that
ClearFormatting followed by putting the formatting and indentation
back works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top