Solved

Changing Document Property Fields in Word with VBA

Posted on 2003-12-10
5
874 Views
Last Modified: 2007-12-19
I have created a number of property fields in the document properties area in a word document.  I want to be able to change the information in each of those fields in a macro.  What is the best way to do this?

Also, on a side note, is there anyway to retrieve information from an exchange server from word.  What I am trying to do, is use the information in exchange to populate the document with the current users details.
0
Comment
Question by:fulama
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:calebS
ID: 9918516
Not sure about the side note, however below is a quick bit of code to change the properties.

' My quick bit of code
With ActiveDocument
    .BuiltInDocumentProperties(wdPropertyTitle) = "My document"
    .BuiltInDocumentProperties(wdPropertySubject) = "Creating a new Macro"
End With

Note, I am assuming you know how to create a macro etc. If you don't, please ask and I will assist.


Also see below a list of other properties.

    .BuiltInDocumentProperties(wdPropertyLastAuthor)
    .BuiltInDocumentProperties(wdPropertyKeywords)
    .BuiltInDocumentProperties(wdPropertyComments)
    .Name
    .Path
    .BuiltInDocumentProperties(wdPropertyTemplate)
    .BuiltInDocumentProperties(wdPropertyTimeCreated)
    .BuiltInDocumentProperties(wdPropertyTimeLastSaved)
    .BuiltInDocumentProperties(wdPropertyLastAuthor)
    .BuiltInDocumentProperties(wdPropertyRevision)
    .BuiltInDocumentProperties(wdPropertyVBATotalEdit)
    .BuiltInDocumentProperties(wdPropertyTimeLastPrinted)
    .BuiltInDocumentProperties(wdPropertyPages)
    .BuiltInDocumentProperties(wdPropertyWords)
    .BuiltInDocumentProperties(wdPropertyCharacters)
    .BuiltInDocumentProperties(wdPropertyParas)
    .BuiltInDocumentProperties(wdPropertyLines)
    .BuiltInDocumentProperties(wdPropertyBytes)
0
 
LVL 9

Expert Comment

by:gilbar
ID: 9920375
if you're on winNT or 2000 or XP, this will make and open a .txt file with user-name's info:
Sub userInfo()
   Dim objWSH
   Dim useName As String
   Dim outFil As String
   useName = "user-Name"
   outFil = "c:\outputFile.txt"
   Set objWSH = CreateObject("WScript.Shell")
   Call objWSH.Run("cmd /c echo y| net user " & useName & " /domain > " & outFil, 0, True)
   Set objWSH = Nothing
   Documents.Open FileName:=outFil
End Sub
0
 
LVL 9

Expert Comment

by:gilbar
ID: 9920412
the windows command 'net user user-name /domain' what gets the info.
IMPORTANT NOTE. . .  in this case, '/domain' is literal, not something to change to your domain name.  it is a switch that tells command to look at your domain, not just your computer, so it has to be typed in as '/domain', not '/microsoft.com' or such
0
 

Author Comment

by:fulama
ID: 9924430
Thanks calebS and gilbar

calebS - Do you know how to reference custom made properties in word?  The code you gave me works for the standard properties, but not for ones that I have made.

gilbar - The code you gave me access the NT user information.  I require access to the Mail (exchange) server to access the Global Address Listing for the current user and to be able to grab things like email address, phone number etc from there and put that in word.
0
 
LVL 1

Accepted Solution

by:
calebS earned 30 total points
ID: 9926659
Exactly the same, except instead of

    .BuiltInDocumentProperties(wdPropertyLastAuthor)


you would say

    .CustomDocumentProperties(myCustomPropertyName)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now