OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Hello all and thanks in advance.
I have a written a MS Excel VBA Macro to change MS Word Document Properties. Why won't any of the other properties besides the "Title" change?
Also, how what code needs to be modified/added so it only opens MS Word once, and then updates each document, and then closes it at the end. If I am running another instance of MS Word, I don't want it or any files open in that instance closed.
The following is sample sheet of what items I have used as an example:
I have a written a MS Excel VBA Macro to change MS Word Document Properties. Why won't any of the other properties besides the "Title" change?
Also, how what code needs to be modified/added so it only opens MS Word once, and then updates each document, and then closes it at the end. If I am running another instance of MS Word, I don't want it or any files open in that instance closed.
The following is sample sheet of what items I have used as an example:
MS Excel VBA to Open MS Word.xlsm | ||||||||
---|---|---|---|---|---|---|---|---|
C | D | E | F | G | H | |||
7 | Directory of Existing Files: | C:\Files\ | ||||||
8 | File Names | File Status | Title | Company | Subject | Author | ||
9 | Test Doc 1.docx | Closed | Test Doc 1 | XYZ Phantom Company | Important Doc 1 | Me | ||
10 | Test Doc 2.docx | Closed | Test Doc 2 | XYZ Phantom Company | Important Doc 2 | Me 2 | ||
11 | Test Doc 3.docx | Closed | Test Doc 3 | XYZ Phantom Company | Important Doc 3 | Me 3 | ||
Sheet1 |
Cells with Data Validation | ||
---|---|---|
Cell | Allow | Criteria |
E8:H8 | List | =Document_Properties |
VBA Code:
Option Explicit
Sub Files_Change_Doc_Properties_Test()
'_______________________________________________________________________________________________________
'Turn off alerts, screen updates, and automatic calculation
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlManual
'_______________________________________________________________________________________________________
'Dimensioning
'Dim Longs
Dim i As Long, j As Long
Dim LastRowNames As Long
Dim LastColDocProp
'Dim Ranges
'Dim Strings
Dim ShtNm As String
Dim FileNm As String
Dim FirstName As String
Dim LastName As String
Dim FullName As String
Dim FolderPath As String
Dim aTitle As String
Dim FolderPath_N_FileName
Dim DocProp As String
Dim DocPropValue As String
'Dim Objects
Dim wApp As Object
'Dim Timer Variables
Dim Benchmark As Double
'Dim Variants
'Dim Word Documents
Dim wDoc As Word.Document
'_______________________________________________________________________________________________________
'Code - Timer Benchmark
Benchmark = Timer
'_______________________________________________________________________________________________________
'Code - Set sheet names
ShtNm = "Sheet1"
'_______________________________________________________________________________________________________
'Code - Get names
FolderPath = Cells(7, 4)
'Function LastRowColF(ByVal SheetName As String, ByVal ColNum As Long) As Long
LastRowNames = LastRowColF(ShtNm, 3)
'Function LastColRowF(ByVal SheetName As String, ByVal RowNum As Long) As Long
LastColDocProp = LastColRowF(ShtNm, 8)
Set wApp = CreateObject("Word.Application")
Set wDoc = wApp.Documents.Add(Template:="Normal")
For i = 9 To LastRowNames
FileNm = Cells(i, 3)
FirstName = Extract_String_Before_F(FileNm, ".")
LastName = Extract_String_After_F(FileNm, ".")
FullName = FirstName & " " & LastName
FolderPath_N_FileName = FolderPath & FileNm
aTitle = FullName
Set wDoc = wApp.Documents.Open(FileName:=FolderPath_N_FileName, ReadOnly:=False, AddToRecentfiles:=False)
wApp.Visible = True
With wDoc
For j = 5 To LastColDocProp
DocProp = Cells(8, j)
DocPropValue = Cells(i, j)
'MsgBox DocProp & ": " & DocPropValue
.BuiltinDocumentProperties(DocProp).Value = DocPropValue
.Close SaveChanges:=True
Next j
End With
Next i
Set wDoc = Nothing: Set wApp = Nothing
'Set wApp = Nothing
'wApp.Visible = True
'_______________________________________________________________________________________________________
'Turn on alerts and screen updates, and calculate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Calculate
'_______________________________________________________________________________________________________
'End of the subroutine/macro
End Sub