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

Poldok

New Member
Joined
Nov 10, 2017
Messages
2
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>
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You could use something along the lines of:
Code:
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.
 
Upvote 0
Thanks Paul,

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

gives me an rror: Run-time error '5' Invalid procedure call or argument.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,083
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top