![]() |
|
|
|
#1
|
||||
|
||||
|
OK. Once the following macro is run, the document is protected as read only - or you could save as PDF. Code:
Sub PopulateCCs()
Const sPath As String = "c:\path\document name.docx" 'the full path of the document
Dim oCC As Object
Dim wdApp As Object
Dim wdDoc As Object
Dim LastRow As Long, lngIndex As Long
Dim xlSheet As Worksheet
Dim sValue As String
Const sPassword As String = "MyPassword"
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
ActiveWorkbook.Save
Set wdDoc = wdApp.Documents.Open(sPath)
If Not wdDoc.ProtectionType = -1 Then
wdDoc.Unprotect Password:=sPassword
End If
wdApp.Visible = True
Set xlSheet = ActiveSheet
With xlSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For lngIndex = 2 To LastRow
For Each oCC In wdDoc.ContentControls
oCC.LockContents = False
If UCase(oCC.Title) = UCase(.Cells(lngIndex, 1)) Then
oCC.Range.Text = .Cells(lngIndex, 2)
Exit For
End If
oCC.LockContents = True
Next oCC
Next lngIndex
End With
wdDoc.Protect Type:=3, Password:=sPassword
lbl_Exit:
Set wdApp = Nothing
Set wdDoc = Nothing
Set oCC = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Importing content control data from excel and populate two fields on selected dropdown
|
badarlodhi | Word VBA | 8 | 02-08-2023 10:47 AM |
Push Word content control data and excel cells
|
shaztastic | Word VBA | 13 | 08-27-2018 06:46 AM |
| How can I set up tab to go back and forth between legacy fields and content control fields in a Word | Ikajed | Word | 1 | 10-13-2017 06:06 PM |
Populate Content Control Dropdowns from Excel
|
Deirdre Kelly | Word VBA | 23 | 09-07-2017 02:51 PM |
| Populate Word Drop-down list with Excel column then auto fill form fields with Excel data | Faldinio | Word VBA | 7 | 10-19-2014 06:03 AM |