Linking Access to word to enter information into template

Shabs

Registered User.
Local time
Today, 14:34
Joined
Sep 2, 2015
Messages
10
Hi,

I am new to this form so please be gentle.

so iam attempting to move information from access into a word document but get the following error.

"Compile error:

User-defined type not defined"

and cant figure out why :banghead:

any help would be much appreciated :rolleyes:

I have set up the text form fields in word which are named.

attxt
sntxt
cntxt
sutxt

My code on my form button is the following
----------------------------------------------------------------------------------
Private Sub cmdprint_Click()

Dim appWord As msWord.Application

Dim doc As Word.Document



On Error Resume Next

Err.Clear


Set appWord = GetObject(, "Word.Application")

If Err.Number <> 0 Then



Set appWord = New Word.Application

End If

Set doc = appWord.Documents.Open("C:\User\Shabir\Desktop\TeacherCS.doc", , True)

With doc

.FormFields("attxt").Result = Me.Reference_Full_Name
.FormFields("sntxt").Result = Me.Reference_Source
.FormFields("cntxt").Result = Me.Teacher_First_Name
.FormFields("sutxt").Result = Me.Teacher_Surname


.Visible = True

.Activate

End With

Set doc = Nothing

Set appWord = Nothing
Exit Sub

errHandler:

MsgBox Err.Number & ": " & Err.Description

End Sub
----------------------------------------------------------------------------
 
Try to change

Code:
 Dim appWord As msWord.Application 
  
  
 Err.Clear
to

Code:
 Dim appWord as Word.Application 
  
  
 Error.clear
Also comment out (') On Error resume next and Error.Clear

so Then you get popup when it goes to error where you can click debug and the line wich is then highlighted is the one who makes the error
 
Try to change

Code:
 Dim appWord As msWord.Application 
  
  
 Err.Clear
to

Code:
 Dim appWord as Word.Application 
  
  
 Error.clear
Also comment out (') On Error resume next and Error.Clear

so Then you get popup when it goes to error where you can click debug and the line wich is then highlighted is the one who makes the error



Unfortunately no luck still get the below .:(

postimg.org/image/czbo60rnn/


(cant post images as not got enough post counts)
 
Activate your word references. Go to VBA editor extra -> references

check Microsoft Word xx.x object library

I also would change to On error Goto errhandler. I did oversee that you have an error handler in your code

Code:
 Private Sub Knop0_Click()
Dim appWord As Word.Application
 Dim doc As Word.Document
  
 On Error GoTo errHandler
 Error.Clear
 
Set appWord = GetObject(, "Word.Application")
 If Err.Number <> 0 Then
  
 Set appWord = New Word.Application
 End If
 Set doc = appWord.Documents.Open("C:\User\Shabir\Desktop\Tea cherCS.doc", , True)
 With doc
 .FormFields("attxt").Result = Me.Reference_Full_Name
.FormFields("sntxt").Result = Me.Reference_Source
.FormFields("cntxt").Result = Me.Teacher_First_Name
.FormFields("sutxt").Result = Me.Teacher_Surname
 
.Visible = True
 .Activate
 End With
 Set doc = Nothing
 Set appWord = Nothing
Exit Sub
 errHandler:
 MsgBox Err.Number & ": " & Err.Description
 End Sub
 
Activate your word references. Go to VBA editor extra -> references

check Microsoft Word xx.x object library

I also would change to On error Goto errhandler. I did oversee that you have an error handler in your code

Code:
 Private Sub Knop0_Click()
Dim appWord As Word.Application
 Dim doc As Word.Document
  
 On Error GoTo errHandler
 Error.Clear
 
Set appWord = GetObject(, "Word.Application")
 If Err.Number <> 0 Then
  
 Set appWord = New Word.Application
 End If
 Set doc = appWord.Documents.Open("C:\User\Shabir\Desktop\Tea cherCS.doc", , True)
 With doc
 .FormFields("attxt").Result = Me.Reference_Full_Name
.FormFields("sntxt").Result = Me.Reference_Source
.FormFields("cntxt").Result = Me.Teacher_First_Name
.FormFields("sutxt").Result = Me.Teacher_Surname
 
.Visible = True
 .Activate
 End With
 Set doc = Nothing
 Set appWord = Nothing
Exit Sub
 errHandler:
 MsgBox Err.Number & ": " & Err.Description
 End Sub

Thanks that's seemed to have fixed all the errors but now I click the button and nothing happens :confused:
 
Do you use office 2013?

I had also little bit problems to get it working.

Here is the code wich works for me in word 2013.

I had to use an old form control in word instead of the normal one to let it work. See pic to show wich i mean

Code:
 Private Sub Knop0_Click()
Dim appWord As Object
Dim doc As Word.Document
 On Error Resume Next
Error.Clear
 Set appWord = GetObject("Word.Application")
 If Error <> 0 Then
     Set appWord = New Word.Application
 End If
 On Error GoTo errHandler
 Set doc = appWord.Documents.Open("C:\Users\Filip\Desktop\Test2.dotx", , True)
appWord.Visible = True
 
With doc
.FormFields("test").Result = Me.Reference_Full_Name
 End With
 Set doc = Nothing
 Set appWord = Nothing
Exit Sub
 errHandler:
 MsgBox Err.Number & ": " & Err.Description
 End Sub
 

Attachments

  • temp.png
    temp.png
    12.1 KB · Views: 109
It works now :) I saved it as a 2007 word doc and all is well, thank you for all your help
 
Do you use office 2013?

I had also little bit problems to get it working.

Here is the code wich works for me in word 2013.

I had to use an old form control in word instead of the normal one to let it work. See pic to show wich i mean

Code:
 Private Sub Knop0_Click()
Dim appWord As Object
Dim doc As Word.Document
 On Error Resume Next
Error.Clear
 Set appWord = GetObject("Word.Application")
 If Error <> 0 Then
     Set appWord = New Word.Application
 End If
 On Error GoTo errHandler
 Set doc = appWord.Documents.Open("C:\Users\Filip\Desktop\Test2.dotx", , True)
appWord.Visible = True
 
With doc
.FormFields("test").Result = Me.Reference_Full_Name
 End With
 Set doc = Nothing
 Set appWord = Nothing
Exit Sub
 errHandler:
 MsgBox Err.Number & ": " & Err.Description
 End Sub

on last question I have generated a button which sends a email to the customer that works fine but the document I have filled the information in from access does not copy across so just sends a blank word document.

how would I do it so it copy's the information into a new document to send across or save the existing one with the information in the documents as appose to in text boxes
 
Save the (.saveas function after filling in the fields) file to a location and then attach it to your mail.
 

Users who are viewing this thread

Back
Top Bottom