I'm trying to use ms Access to open up ms word, create formfield and tables but always end up empty. 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. 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
CodePudding user response:
Considering comments, issue is likely due to a Word process still running in background even though not showing in Task Manager. This can be caused by unqualified references to Word objects.
Prefix appword.
in front of every Selection
and ActiveDocument
usage and code should then run every time. It does for me now.
This can also happen with automation code for other Office apps - Excel, Outlook, PowerPoint, etc.