简体   繁体   English

"MS Access 中的 MS Word vba"

[英]MS Word vba in MS Access

I'm trying to use ms Access to open up ms word, create formfield and tables but always end up empty.我正在尝试使用 ms Access 打开 ms word,创建表单域和表格,但最终总是空的。 I tried using the same code but replacing the entire code between "with doc" to .formfields("TxtDate").result = me.txt2 and is able to transfer whatever i typed in txt2 into the formfield located in ms word.我尝试使用相同的代码,但将“with doc”之间的整个代码替换为 .formfields("TxtDate").result = me.txt2 并且能够将我在 txt2 中输入的任何内容传输到位于 ms word 中的表单字段中。 Thus I'm unsure which portion of the code went wrong.因此,我不确定代码的哪一部分出错了。 Would like to seek help on my code.想就我的代码寻求帮助。 Thanks谢谢

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

Often the issue is there is a Word process still running in background even though not showing in Task Manager.通常的问题是,即使没有在任务管理器中显示,Word 进程仍在后台运行。 This can be caused by unqualified references to Word objects.这可能是由对 Word 对象的不合格引用引起的。

Prefix appword.前缀appword. in front of every Selection and ActiveDocument usage and code should then run every time.在每个SelectionActiveDocument使用之前,代码应该每次都运行。 It does for me now.现在对我有用。

This can also happen with automation code for other Office apps - Excel, Outlook, PowerPoint, etc.这也可能发生在其他 Office 应用程序(Excel、Outlook、PowerPoint 等)的自动化代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM