出色的VBA字形:为何/为什么未定义dim? [英] VBA word form to excel: How/why are the dim's undefined?

查看:162
本文介绍了出色的VBA字形:为何/为什么未定义dim?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA的新手(并且从3个月开始学习代码:),并且似乎无法使该代码正常工作.我正在尝试将表格数据(Word)获取到Excel并从youtube视频中复制此代码.由于某种原因,昏暗的灯不起作用(也许是因为我在Mac上工作?). 新的Word.Application(第2行),Word.Document(第3行),Word.ContentControl(第4行)给出了错误:用户定义的类型未定义.

I'm new to VBA (and learning code since 3 months:)) and cannot seem to get this code working. I'm trying to get form data (Word) to Excel and copied this code from a youtube video. For some reason, the dim's do not work (maybe because I'm working on a mac?). New Word.Application (line 2), Word.Document (line 3), Word.ContentControl (line 4) give an error: User-Defined Type Not Defined.

希望你们中的一个可以帮助我:)

Hopefully one of you can help me out:)

这是代码:

Sub getWordFormData()
Dim wdApp As New Word.Application
Dim myDoc As Word.Document
Dim CCtl As Word.ContentControl
Dim myFolder As String, strFile As String
Dim myWkSht As Worksheet, i As Long, j As Long

myFolder = "/Users/me/Desktop/survey"
Application.ScreenUpdating = False

If myFolder = "" Then Exit Sub
Set myWkSht = Activesheet
Activesheet.Cells.Clear

Range("A1") = "Name"
Range("B1") = "Question 1"
Range("C1") = "Comments"
Range("D1") = "Question 2"
Range("E1") = "Comments"
Range("F1") = "Question 3"
Range("G1") = "Comments"

i = myWkSht.Cells(myWkSht.Rows.Count, 1).End(xlUp).Row
strFile = Dir(myFolder & "\*.docx", vbNormal)

While strFile <> ""
i = i + 1

Set myDoc = wdApp.Documents.Open(fileName:=myFolder & "\" & strFile,         AddToRecentFiles:=False, Visible:=False)

With myDoc
j = 0
For Each CCtl In .contentControls
j = j + 1
myWkSht.Cells(i, j) = CCtl.Range.Text
Next
myWkSht.colums.AutoFit
End With
myDoc.Close SaveChanges:=False
strFile = Dir()
Wend
wdApp.Quit
Set myDoc = Nothing: Set wdApp = Nothing: Set myWkSht = Nothing
Application.ScreenUpdating = True

End Sub

推荐答案

此代码使用了称为早期绑定的东西,这意味着已对库(在这种情况下为Microsoft Word)进行了引用-这具有一些优点,例如允许使用在VBE中.要使代码正常工作,您有两种选择:

This code uses something called Early Binding which means a reference has been made to a library (in this case, Microsoft Word) - This has some advantages such as allowing the use of intellsense within the VBE. To get the code working you have two options:

1..将代码转换为使用后期绑定-例如:

1. Convert the code to use Late Binding - for example:

Sub getWordFormData()
Dim wdApp As Object
Set wdApp = CreateObject("Word.Application")


2..设置对Microsoft Word对象库的引用:


2. Set a reference to the Microsoft Word Objects library:

  • 在VBE中,转到工具"->引用".

  • In the VBE go to Tools -> References.

找到Microsoft Word xx.x Object Library并勾选左侧的复选框.

Find Microsoft Word xx.x Object Library and tick the checkbox on the left.

按确定",转到调试"->编译"

Press OK, the go to Debug -> Compile

有关绑定的一些信息:

在自动化中使用早期绑定和后期绑定-MSDN
VBA参考和早期绑定相对于后期绑定-Excel事项

NB: :刚看到此评论:

NB: Just saw this comment:

也许是因为我正在使用Mac?"

"maybe because I'm working on a mac?"

在这种情况下,您将必须使用Early Binding,因为ActiveX在Mac上无法使用-因此,您可以选择选项2.但是Content Controls在Mac OS上也不起作用,因此您必须改用替代方法

In which case you will have to use Early Binding, because ActiveX doesn't work on Mac - so Option 2 for you. But Content Controls don't work on Mac OS either so you will have to use a workaround instead

这篇关于出色的VBA字形:为何/为什么未定义dim?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆