Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word - Get Simple Document Properties 1

Status
Not open for further replies.

Jables

Programmer
Aug 28, 2001
148
US
I need to use VBA to get some simple properties from the currently open MS Word 2000 document, and then export the values to an Excel Spreadsheet Template.

I want to be able to do this using a Hotkey.

In more specific terms:

I want to press Ctrl + W.
When this occurs, I want VBA to grab the following properties:
1. Number of Lines in the document.
2. Number of Characters (without spaces) in the document.
3. Number of Words in the document.
*Note: these are all available under the File >> Properties menu item under the tab called "Statistics"

Then these properties should be exported out into empty, predetermined cells in an Excel Template.

Can this be done with a simple Macro, or is VBA required. If so, no problem. And if so, anyone got any ideas?
 
Place this VBA code in a module.

Sub ExportProperties()

Dim oExcel As New Excel.Application
Dim oWkbk As Excel.Workbook
Dim oWksht As Excel.Worksheet

Set oExcel = CreateObject("Excel.application")
Set oWkbk = oExcel.Workbooks.Add
Set oWksht = oWkbk.Worksheets(1)
With oWksht
.Cells(1, 1) = "Words: " & Application.ActiveDocument.BuiltInDocumentProperties(wdPropertyWords)
.Cells(1, 2) = "Lines: " & Application.ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
.Cells(1, 3) = "Char: " & Application.ActiveDocument.BuiltInDocumentProperties(wdPropertyCharacters)
End With
oWkbk.SaveAs "C:\Temp\Test.xls"

Set oWksht = Nothing
Set oWkbk = Nothing
Set oExcel = Nothing

End Sub

Now right-click on the toolbar, choose Customize from the shortcut menu, click on the Keyboard button, in the Categories combo box choose Macros, in the Commands combo box choose ExportProperties, click in the New Shortcut Key text box, hit ctrl-W, click on the Assign button and you are done!!! Note that the function has no error trapping, and you will have to replace C:\Temp\test.xls with your own file path and name. You have also overwridden the default behavior for the ctrl-W key combination.
 
Yes indeed sir. You certainly made short work of that. This works great. Thanks for the information.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top