EugeneGough-2857 avatar image
0 Votes"
EugeneGough-2857 asked EugeneGough-2857 commented

How to add a Word.WdFieldType to a word table with

I am creating a document that needs a header with the build date, page number and total pages. Word has several functions that will do that but attempts to use them in a table causes a run time error from Interop,
{"This command is not available."} Note that I know the table is valid as the "test" shows up in the proper cell if the add field is disabled.

Simple example of the failure

Dim tbrng As Word.Range = table1.Rows(rowCount).Cells(1).Range
tbrng.Fields.Add(tbrng, Word.WdFieldType.wdFieldDate)

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Additional information. The following code also fails with interop statement that the command is not available.

    Dim rng11 As Word.Range = table1.Rows(1).Cells(1).Range
     MsgBox("Cell 1,1 Range" & vbNewLine & rng11.Start.ToString & "," & rng11.End.ToString)
     rng11.Fields.Add(rng11, Word.WdFieldType.wdFieldDate) 

    The msgbox reports that the cell range is (0,1).

It really blows my mind that there doesn't seem to be a way to put automatic DATE update, Page Number and Total pages information into a Word header. My first attempted resulted in being told by members of this audience that I had to put a table into the header in order to separate the fields to prevent the add.field function from removing any previous data. Now I find out that the table "rules" apparently don't allow the add field functions. Are the developers trying to say that nobody wants to have both page numbers and date last changed information in headers?

0 Votes 0 ·

Did you check if a similar code works in VBA?

Instead of building the document programmatically, maybe you can load an existing template document (made manually), and then insert, adjust or hide some elements.

0 Votes 0 ·

Thanks, one of the reasons I was trying to do this way was to eliminate the need for using a template or having to depend on a model document with VBA in it. I could use to stuff a VBA routine into a document (have done that before) but again, not what I wanted.

It really is un-reasonable how difficult it is to find documentation on Word Interop methods, etc. That and many of the examples are taylored to VBA only and it is a hit and miss effort to guess what the equivalent VB.Net method would look like. Why would the designers the field.add methods that insist on being the only content of a range? Why would the Table implementation prevent the use of field.add?

Oh well, tis Saturday and time to forget this stuff for a while.

Thanks again, I will continue to look.................

0 Votes 0 ·

0 Answers