Fill word with Ms Access

ZKHADI

Member
Local time
Today, 07:18
Joined
Apr 5, 2021
Messages
118
hey guys ! need help!

i got a code and i created a word document with textboxes.
i paste the code but it have some error could anybody fix it?
this code is about old version of Microsoft office and i am using 2013 .
see below image and check code
 

Attachments

  • Untitled.png
    Untitled.png
    135.6 KB · Views: 212
Copy/paste code into post between CODE tags, not image.
 
Code:
Function fillwordform()
Dim appword As word.Application
Dim doc As word.Document
Dim Path As String
On Error Resume Next
Error.Clear Path = "D:\College Database\Documents\CharacterCertificate.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, True)
With doc
.FormFields("StudentName").Result = Me.Student_name
'.FormFields("ClassNo").Result = Me
.FormFields("RegistrationNo").Result = Me.Board_University_Registration_No
.FormFields("Faculty").Result = Me.CurrentFacultySemester
.FormFields("Status").Result = Me.StudentStatus
End With
appWord.Visible = True
appWord.Activate
Set doc = Nothing
Set appWord = Nothing
End Function
 
you add Reference to Microsoft Word XX.XX Object Library in VBA.
 
press Ctrl-G to go to VBA.
on Menu->Tools->References, you add
Microsoft Word XX.X Object Library (XX.X is the version number of your office).
 
it does not open, because you have Error there.
try this code:
Code:
Function fillwordform()
Dim appword As word.Application
Dim doc As word.Document
Dim Path As String
'On Error Resume Next
Error.Clear
Path = "D:\College Database\Documents\CharacterCertificate.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, True)
With doc
.FormFields("StudentName").Result = Me.Student_name
'.FormFields("ClassNo").Result = Me
.FormFields("RegistrationNo").Result = Me.Board_University_Registration_No
.FormFields("Faculty").Result = Me.CurrentFacultySemester
.FormFields("Status").Result = Me.StudentStatus
End With
appWord.Visible = True
appWord.Activate
Set doc = Nothing
Set appWord = Nothing
End Function
 
it does not open, because you have Error there.
try this code:
Code:
Function fillwordform()
Dim appword As word.Application
Dim doc As word.Document
Dim Path As String
'On Error Resume Next
Error.Clear
Path = "D:\College Database\Documents\CharacterCertificate.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, True)
With doc
.FormFields("StudentName").Result = Me.Student_name
'.FormFields("ClassNo").Result = Me
.FormFields("RegistrationNo").Result = Me.Board_University_Registration_No
.FormFields("Faculty").Result = Me.CurrentFacultySemester
.FormFields("Status").Result = Me.StudentStatus
End With
appWord.Visible = True
appWord.Activate
Set doc = Nothing
Set appWord = Nothing
End Function

problem fixed but the file open with empty textboxes.........
 

Attachments

  • Untitled.png
    Untitled.png
    86.7 KB · Views: 186
google and use Bookmark in Word.
 
google and use Bookmark in Word.
Good morning.
I have been using the Function "FillwordForm" for many, many years and it has suddenly stopped working.
I have been beating my head against the wall trying find out why.
When the routine is run Word opens but with a grayed-out screen but no form. See attachment 1

I have put a break in the code and single stepped through it and also reentered lines of code to ensure that the pre-compiler is working.
Upon reviewing the VBA code in Attachment 2 it appears that the Item "Doc" is not being populated as hovering over it shows value is "Nothing"
Similarly in the Debug.Print statement nothing is returned.

I have checked the Word form that it references and it is in the correct place and the bookmarks are correct.

I have the same problem being exhibited on three different PC's

Can anyone please suggest why?
 

Attachments

  • AA FillwordForm Attachment 1.JPG
    AA FillwordForm Attachment 1.JPG
    73.3 KB · Views: 99
  • AA FillwordForm Issue Attachment 2.JPG
    AA FillwordForm Issue Attachment 2.JPG
    203.2 KB · Views: 107
Last edited:
try commenting out some codes:
Code:
    ...
    ...
    ...
    
    'Set appword = GetObject("word.application")
    
    'If Err.Number <> 0 Then
        Set appword = CreateObject("Word.Application")
        appword.Visible = True
    'End If
    
    ...
    ...
    ...
    
    'Set doc = Nothing
    'Set appword = Nothing
 
Thanks Arnelgp
I commented out quite a few lines as you suggested and also added some Debug.Print statements.
If you look in the immediate window you will see that "Doc" does not contain anything???
Hovering over it in debug mode contents are always 'Nothing'
 

Attachments

  • AA FillwordForm Attachment 3.JPG
    AA FillwordForm Attachment 3.JPG
    175.2 KB · Views: 98
debug.print doc, won't show you anything since doc is an Object.
try to Step through the code.

also comment out:

'On Error Resume Next

so you can See what is the error.
 
do you MS Word installed?
check the name and the path of of your word document.
do you have the path name correct and the filename correct?
 
Hi Arnelgp.
Apologies for the delay. I have been working on other issues (Friday night drinks).
The good news is the problem is solved :)
After a lot of checking I eventually found that I had a Folder on Drive C: called 'swshare' and also a legacy share drive called 'swshare'.
I was working on word documents in swshare on the C: drive but it was somehow or other still looking at the old shared folder.
Once again a big thank you for all your help.

a
 

Users who are viewing this thread

Back
Top Bottom