# How do I change field property in Word from Excel VBA?



## Poldok (Nov 10, 2017)

Hi,

first, let me say that I am not a great programmer, just reusing/modifying googled code.

I have created a macro in Excel that copies some data from its Excel file to an existing Word file. 

Now, I would like to change a few *field properties* of the Word file ... you know, such as Author, Revision, Keywords ... from the same macro, using values in the worksheet.

I have searched google with no much success, can somebody explain me whether it is possible and give me (or pointing to) some examples.

Many thanks.

P.S.
I play a bit with BuiltInDocumentProperties, for example
<code>Dim propertylist as DocumentProperties

Set propertylist = myDoc.BuiltinDocumentProperties
Set propertylist.Name("Author") = pssa50.Sheets("Header").Range("D22").Value

where,<code>of course,</code> myDoc is the reference to my Word file and pssa50 <code>is the reference to my Excel file</code></code>


----------



## Macropod (Nov 10, 2017)

You could use something along the lines of:

```
Sub Demo()
'Note: A reference to the Word library must be set, via Tools|References
Dim wdApp As New Word.Application, wdDoc As Word.Document, xlwkSht As Worksheet, i As Long
Const StrDocNm As String = "Full document name & path"
If Dir(StrDocNm) = "" Then Exit Sub
Set xlwkSht = Workbooks("pssa50").Sheets("Header")
Set wdDoc = wdApp.Documents.Open(Filename:=StrDocNm, ReadOnly:=False, AddToRecentfiles:=False)
With wdDoc
  'Available BuiltinDocumentProperties:
  'Application name, Author, Category, Comments, Company, Content status, _
  Content type, Creation date, Document version, Format, Hyperlink base, _
  Keywords, Language, Last author, Last print date, Last save time, Manager, _
  Number of bytes, Number of characters, Number of characters (with spaces), _
  Number of hidden Slides, Number of lines, Number of multimedia clips, _
  Number of notes, Number of pages, Number of paragraphs, Number of slides, _
  Number of words, Revision number, Security, Subject, Template, Title, Total editing time
  .BuiltinDocumentProperties("Author").Value = xlwkSht.Range("D22").Value
  .Close SaveChanges:=True
End With
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
```
Note that I've listed all the std properties. Not all of these can be overwritten, though. For example, if you try to write something to 'Number of words', then output that property's value to a message box, Word will return the actual value, not what you wrote to it.


----------



## Poldok (Nov 11, 2017)

Thanks Paul,

however,
.BuiltinDocumentProperties("Author").Value = xlwkSht.Range("D22").Value

gives me an rror:  Run-time error '5'    Invalid procedure call or argument.


----------



## Macropod (Nov 11, 2017)

The code as posted works fine for me.

While you're testing, you might want to set:
wdApp.Visible = True
before:
With wdDoc
That way, you won't end up with orphaned Word sessions.


----------

