Write text in table within textbox placed in pagefooter

P

pmm

Hi

I have a problem with a table/texbox object placed in pagefooter. Se
the
description and code here:


'-------------------------------------------------------------------------
' WANT: Replace existing text with new text - format it - in the
pagefooter,
' and the text shall be place in a table there is place in a
textbox
' (there are already added a textbox and inside this a table:
' one row and one coloum).
' COMMENT:
' It works fine when the texbox and table are placed on the
page
' PROBLEM:
' It can't find the shape in pagefooter, and I don't know how to
get it.
' (it shall be a shape and not at frame)
'-------------------------------------------------------------------------
Sub ReplaceText_TableTextbox()

Dim txt_adress As Range

With ActiveDocument

Set txt_adress = .Shapes(1).TextFrame.TextRange

txt_adress.Tables(1).Cell(1, 1).Range.Delete
txt_adress.Tables(1).Cell(1, 1).Range = "This is the first line"
txt_adress.Tables(1).Cell(1, 1).Range.InsertParagraphAfter
txt_adress.Tables(1).Cell(1, 1).Range.InsertAfter "This is the
second line"
txt_adress.Tables(1).Cell(1, 1).Range.InsertParagraphAfter
txt_adress.Tables(1).Cell(1, 1).Range.InsertAfter "This is the last
line"

'Line 1: Bold and spaceafter 10
txt_adress.Paragraphs(1).Range.Bold = True
txt_adress.Paragraphs(1).Range.ParagraphFormat.SpaceAfter = 10
'Line 2: Font size 16
txt_adress.Paragraphs(2).Range.Font.Size = 16

End With

End Sub
 
H

Helmut Weber

Hi,

look for a shaperange in the footer,
not for a shape, like

Sub Macro8()
With ActiveDocument.StoryRanges(wdPrimaryFooterStory)
MsgBox .ShapeRange.Count
End With
End Sub

Others have struggled with this as well.

At least I don't know
why one must distinguish between shape and shaperange.
You may add a shape in a footer,
yet, there is no shape, but a shaperange. :-(

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jean-Guy Marcil

pmm said:
Hi

I have a problem with a table/texbox object placed in pagefooter. Se
the
description and code here:


'-------------------------------------------------------------------------
' WANT: Replace existing text with new text - format it - in the
pagefooter,
' and the text shall be place in a table there is place in a
textbox
' (there are already added a textbox and inside this a table:
' one row and one coloum).
' COMMENT:
' It works fine when the texbox and table are placed on the
page
' PROBLEM:
' It can't find the shape in pagefooter, and I don't know how to
get it.
' (it shall be a shape and not at frame)
'-------------------------------------------------------------------------

A few comments about coding in general.

Try not to use default properties like:

txt_adress.Tables(1).Cell(1, 1).Range = "This is the first line"

Here, you meant to use the .Text property of the range object to insert
text. It is just a good habit to actually type the property because we have
no control over the object model that Microsoft uses. While it is safe to
assume that most default properties will not be changed, one must never say
never, unless your name is James Bond! Also, it is possible that default
properties will become inadmissible in later releases of Word...

Also, try to use With blocks to simplify the code maintenance, to make it
easier to read and to make it faster to execute (the compiler does not have
to rebuild each statement).

In your case, since you want to deal with an object in a footer, you must
remember that a footer belongs to a particular section and that there are
three types of footers (the same applies to headers). The code I suggest will
work with the main footer of the first section in the document.

Finally, you do not need to delete the specified range if you are going to
replace it all by something new. See my code for an example.


Sub ReplaceText_TableTextbox()

Dim txt_adress As Range

Set txt_adress = ActiveDocument.Sections(1). _
Footers(wdHeaderFooterPrimary).Range.ShapeRange(1). _
TextFrame.TextRange

With txt_adress.Tables(1).Cell(1, 1).Range
.Text = "This is the first line" & Chr(13) & _
"This is the second line" & Chr(13) & _
"This is the last line"
'Line 1: Bold and spaceafter 10
With .Paragraphs(1).Range
.Bold = True
.ParagraphFormat.SpaceAfter = 10
End With
'Line 2: Font size 16
.Paragraphs(2).Range.Font.Size = 16
End With

End Sub


As a final thought, may I ask why you are using a 1x1 table in a text box?
This may lead to corruption and makes the whole thing more difficilt to
handle. You could use a simple text box, and then use code like:


Sub ReplaceText_Textbox()

Dim txt_adress As Range

Set txt_adress = ActiveDocument.Sections(1). _
Footers(wdHeaderFooterPrimary).Range.ShapeRange(1). _
TextFrame.TextRange

With txt_adress
.Text = "This is the first line" & Chr(13) & _
"This is the second line" & Chr(13) & _
"This is the last line"
'Line 1: Bold and spaceafter 10
With .Paragraphs(1).Range
.Bold = True
.ParagraphFormat.SpaceAfter = 10
End With
'Line 2: Font size 16
.Paragraphs(2).Range.Font.Size = 16
End With

End Sub
 

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