Solved

Changing Document Property Fields in Word with VBA

Posted on 2003-12-10
5
888 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

733 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