[英]MS Word vba in MS Access
我正在尝试使用 ms Access 打开 ms word,创建表单域和表格,但最终总是空的。 我尝试使用相同的代码,但将“with doc”之间的整个代码替换为 .formfields("TxtDate").result = me.txt2 并且能够将我在 txt2 中输入的任何内容传输到位于 ms word 中的表单字段中。 因此,我不确定代码的哪一部分出错了。 想就我的代码寻求帮助。 谢谢
Function FillWordForm()
Dim appword as Word.Application
Dim doc as Word.Document
Dim Path as String
On Error Resume Next
Error.Clear
Path = "H:\project delta\test.docx"
Set appword = GetObject(, "word.application")
If Err.Number <> 0 then
Set appword = New Word.Application
appword.Visible = true
End if
Set doc = appword.Documents.open(Path, , False)
With Doc
Selection.TypeParagraph
Selection.FormFields.Add Range:=Selection.Range, Type:= _
WdFieldFormTextInput
ActiveDocument.FormFields.Shaded = Not ActiveDocument.FormFields.Shaded
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=13, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = True
.Cell(1,1).Select
Selection.TypeText Text:="S/N"
.Cell(1,2).Select
Selection.TypeText Text:="Package Title"
.Cell(1,3).Select
Selection.TypeText Text:="Reference"
.Cell(1,4).Select
Selection.TypeText Text:="Month"
End With
appword.Visible = True
appword.Activate
End With
Set doc = Nothing
Set appword = Nothing
End Function
通常的问题是,即使没有在任务管理器中显示,Word 进程仍在后台运行。 这可能是由对 Word 对象的不合格引用引起的。
前缀appword.
在每个Selection
和ActiveDocument
使用之前,代码应该每次都运行。 现在对我有用。
这也可能发生在其他 Office 应用程序(Excel、Outlook、PowerPoint 等)的自动化代码中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.