Hello!
I hope this is the right newsgroup I'm posting this question. I'm having a
problem when adding Page Number field to a table's cell. That table is placed
in the document's header. The document is generated by COM Interop with
VB.NET. The error description is below, followed by the source code that
generate the document.
Unhandled Exception: System.Runtime.InteropServices.COMException
(0x800A11FD): This command is not available
at Word.Fields.Add(Range Range, Object& Type, Object& Text, Object&
PreserveFormatting)
at
WEG.Applications.CRM.Proposta.DocumentBuilder.HeaderWriter.WritePageNumbers(Document
document, Cell cell) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.CRM.Proposta.DocumentBuilder\DocumentBuilder\Implementation\SectionWriter\HeaderWriter.vb:line 93
at
WEG.Applications.CRM.Proposta.DocumentBuilder.HeaderWriter.WriteTable(Document
document, IProposta proposta) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.CRM.Proposta.DocumentBuilder\DocumentBuilder\Implementation\SectionWriter\HeaderWriter.vb:line 35
at
WEG.Applications.CRM.Proposta.DocumentBuilder.HeaderWriter.Write(Documentdocument,
IProposta proposta) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.CRM.Proposta.DocumentBuilder\DocumentBuilder\Implementation\SectionWriter\HeaderWriter.vb:line 9
at
WEG.Applications.CRM.Proposta.DocumentBuilder.DocumentWriter.Write(Document
document, IComposition composition, IProposta proposta) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.CRM.Proposta.DocumentBuilder\DocumentBuilder\Implementation\DocumentWriter.vb:line 12
at
WEG.Applications.CRM.Proposta.DocumentBuilder.WordDocumentFactory.Create(IProposta
proposta, IModel model) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.CRM.Proposta.DocumentBuilder\DocumentBuilder\Factory\WordDocumentFactory.vb:line
12
at DocumentBuilderLauncher.EntryPoint.Launch(OracleConnection connection)
in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Tests.Weg.Applications.CRM.Proposta.DocumentBuilder.Launcher\EntryPoint.vb:line 25
at DocumentBuilderLauncher.EntryPoint.Main() in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Tests.Weg.Applications.CRM.Proposta.DocumentBuilder.Launcher\EntryPoint.vb:line 10
Imports System.Globalization
Imports ABC.Globalization.Translator
Friend Class HeaderWriter
Implements ISectionWriter
Public Sub Write(ByVal document As Word.Document, ByVal proposal As
IProposal) Implements ISectionWriter.Write
WriteTitle(document, proposal)
WriteTable(document, proposal)
InsertCompanyLogoImage(document, proposal.GetLanguageCulture())
If Not proposal.SentToCustomer Then
InsertProposalNotSentStampImage(document, proposal.GetLanguageCulture())
End Sub
Private Sub WriteTitle(ByVal document As Word.Document, ByVal proposal
As IProposal)
Dim writer As New TextWriter(GetDocumentHeader(document))
writer.WriteParagraph(proposal.Company.Name, True,
DocumentFormatter.BigFontSize)
writer.WriteParagraph()
writer.WriteParagraph()
writer.WriteParagraph()
writer.WriteParagraph()
writer.WriteParagraph()
End Sub
Private Sub WriteTable(ByVal document As Word.Document, ByVal proposal
As IProposal)
Dim table As Word.Table = New
TableFactory(GetDocumentHeader(document)).Create(1, 3)
SetupColumnsWidth(table)
SetupCellsAlignment(table)
SetupRowHeight(table)
PutBordersOnTopAndBottom(table)
WriteProposalId(table.Cell(1, 1), proposal)
WriteEmissionDate(table.Cell(1, 2), proposal)
WritePageNumbers(document, table.Cell(1, 3))
End Sub
Private Sub InsertCompanyLogoImage(ByVal document As Word.Document,
ByVal culture As CultureInfo)
Dim imageFilePath As String = New
ImageStore(culture).GetImageFilePath("Logo")
Dim shape As Word.Shape =
GetDocumentHeader(document).Shapes.AddPicture(imageFilePath)
shape.Left = 450
End Sub
Private Sub InsertProposalNotSentStampImage(ByVal document As
Word.Document, ByVal culture As CultureInfo)
Dim imageFilePath As String = New
ImageStore(culture).GetImageFilePath("Stamp")
Dim shape As Word.Shape =
GetDocumentHeader(document).Shapes.AddPicture(imageFilePath)
shape.WrapFormat.Type = Word.WdWrapType.wdWrapNone
shape.PictureFormat.ColorType =
Microsoft.Office.Core.MsoPictureColorType.msoPictureWatermark
shape.Left = document.PageSetup.PageWidth / 2 - shape.Width / 2
shape.Top = document.PageSetup.PageHeight / 2 - shape.Height / 2
End Sub
Private Sub SetupColumnsWidth(ByVal table As Word.Table)
table.Columns.Item(1).SetWidth(260.2F, Word.WdRulerStyle.wdAdjustNone)
table.Columns.Item(2).SetWidth(130.0F, Word.WdRulerStyle.wdAdjustNone)
table.Columns.Item(3).SetWidth(130.0F, Word.WdRulerStyle.wdAdjustNone)
End Sub
Private Sub SetupCellsAlignment(ByVal table As Word.Table)
table.Cell(1, 1).VerticalAlignment =
Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter
table.Cell(1, 2).VerticalAlignment =
Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter
table.Cell(1, 3).VerticalAlignment =
Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter
table.Cell(1, 3).Range.Paragraphs.First.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphRight
End Sub
Private Sub SetupRowHeight(ByVal table As Word.Table)
table.Rows.Height = 20
table.Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightExactly
End Sub
Private Sub PutBordersOnTopAndBottom(ByVal table As Word.Table)
table.Borders.Item(Word.WdBorderType.wdBorderBottom).LineStyle =
Word.WdLineStyle.wdLineStyleSingle
table.Borders.Item(Word.WdBorderType.wdBorderTop).LineStyle =
Word.WdLineStyle.wdLineStyleSingle
End Sub
Private Sub WriteProposalId(ByVal cell As Word.Cell, ByVal proposal As
IProposal)
cell.Range.InsertAfter(Translate("PROPOSAL") & ": " &
proposal.GetId())
End Sub
Private Sub WriteEmissionDate(ByVal cell As Word.Cell, ByVal proposal As
IProposal)
Dim emissionDate As Date
If proposal.DataEmissao > Date.MinValue Then 'REFACTOR: make clear
that this is checking whether the proposal has emission date
emissionDate = proposal.DataEmissao
Else
emissionDate = Date.Today
End If
cell.Range.InsertAfter(Translate("DATE") & ": " &
emissionDate.ToString("d", proposal.GetLanguageCulture().DateTimeFormat))
End Sub
Private Sub WritePageNumbers(ByVal document As Word.Document, ByVal cell
As Word.Cell)
cell.Range.Collapse(Word.WdCollapseDirection.wdCollapseStart)
document.Fields.Add(cell.Range, Word.WdFieldType.wdFieldNumPages)
cell.Range.Collapse(Word.WdCollapseDirection.wdCollapseStart)
cell.Range.InsertAfter(" / ")
cell.Range.Collapse(Word.WdCollapseDirection.wdCollapseStart)
document.Fields.Add(cell.Range, Word.WdFieldType.wdFieldPage)
cell.Range.Collapse(Word.WdCollapseDirection.wdCollapseStart)
cell.Range.InsertAfter(Translate("PAGE") & ": ")
End Sub
Private Function GetDocumentHeader(ByVal document As Word.Document) As
Word.HeaderFooter
Return
document.Sections.Item(1).Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary)
End Function
Public Function HasSomethingToWrite(ByVal proposal As IProposal) As
Boolean Implements ISectionWriter.HasSomethingToWrite
Return True
End Function
End Class
Doug Robbins - Word MVP - 22 Dec 2004 10:35 GMT
The easiest thing to do is have your code create a new document from a
template that already contains the table in the header with the page number
field in it.

Signature
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
> Hello!
>
[quoted text clipped - 9 lines]
> PreserveFormatting)
> at
WEG.Applications.CRM.Proposta.DocumentBuilder.HeaderWriter.WritePageNumbers(
Document
> document, Cell cell) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.C
RM.Proposta.DocumentBuilder\DocumentBuilder\Implementation\SectionWriter\Hea
derWriter.vb:line 93
> at
WEG.Applications.CRM.Proposta.DocumentBuilder.HeaderWriter.WriteTable(Docume
nt
> document, IProposta proposta) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.C
RM.Proposta.DocumentBuilder\DocumentBuilder\Implementation\SectionWriter\Hea
derWriter.vb:line 35
> at
WEG.Applications.CRM.Proposta.DocumentBuilder.HeaderWriter.Write(Documentdoc
ument,
> IProposta proposta) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.C
RM.Proposta.DocumentBuilder\DocumentBuilder\Implementation\SectionWriter\Hea
derWriter.vb:line 9
> at
WEG.Applications.CRM.Proposta.DocumentBuilder.DocumentWriter.Write(Document
> document, IComposition composition, IProposta proposta) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.C
RM.Proposta.DocumentBuilder\DocumentBuilder\Implementation\DocumentWriter.vb
:line 12
> at
WEG.Applications.CRM.Proposta.DocumentBuilder.WordDocumentFactory.Create(IPr
oposta
> proposta, IModel model) in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Weg.Applications.C
RM.Proposta.DocumentBuilder\DocumentBuilder\Factory\WordDocumentFactory.vb:l
ine
> 12
> at DocumentBuilderLauncher.EntryPoint.Launch(OracleConnection connection)
> in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Tests.Weg.Applicat
ions.CRM.Proposta.DocumentBuilder.Launcher\EntryPoint.vb:line 25
> at DocumentBuilderLauncher.EntryPoint.Main() in
C:\Dev\Proj\Weg.Applications.CRM.Proposta.DocumentBuilder\Tests.Weg.Applicat
ions.CRM.Proposta.DocumentBuilder.Launcher\EntryPoint.vb:line 10
> Imports System.Globalization
> Imports ABC.Globalization.Translator
[quoted text clipped - 126 lines]
> Word.HeaderFooter
> Return
document.Sections.Item(1).Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFoot
erPrimary)
> End Function
>
[quoted text clipped - 3 lines]
> End Function
> End Class