簡體   English   中英

"MS Access 中的 MS Word vba"

[英]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. 在每個SelectionActiveDocument使用之前,代碼應該每次都運行。 現在對我有用。

這也可能發生在其他 Office 應用程序(Excel、Outlook、PowerPoint 等)的自動化代碼中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM