-
Word form data (content controls) to Excel
Hi
I read a few posts but cannot get the code to work. I'm using the code from a couple of posts by Macropod (Paul). I can't link to the previous posts.
Sub GetFormData()
Application.ScreenUpdating = False
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim CCtrl As Word.ContentControl
Dim strFolder As String, strFile As String
Dim WkSht As Worksheet, i As Long, j As Long
strFolder = "c:\users\myname\desktop\answers"
If strFolder = "" Then Exit Sub
Set WkSht = ActiveSheet
i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
strFile = Dir(strFolder & "\RESOURCES QUESTIONNAIRE.docx", vbNormal)
While strFile <> ""
i = i + 1
Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
j = 0
For Each CCtrl In .ContentControls
j = j + 1
WkSht.Cells(i, j) = CCtrl.Range.Text
Next
End With
wdDoc.Close SaveChanges:=False
strFile = Dir()
Wend
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub
I get the error "Compile error" user-defined type not defined" on line 3 "Dim wdApp As New Word.Application"
I know it's something silly I'm not doing in changing in the code from being generic to specific in my case. I have the word file open but I don't believe that's the problem.
The word doc with the answers is: c:\users\desktop\myname\RESOURCES QUESTIONNAIRE.docx
The excel file is in the same folder (diff name) c:\users\desktop\myname\RESOURCES QUESTIONNAIRE-Answers.xlsm
Thanks
Stef
-
Under Tools / References, I have enabled "Microsoft Office 16.0 Object Library".
Just as I typed this I realised my problem. I hadn't enabled Microsoft Word 16.0 Object Library"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules