title: Creating Word documents from templates in Access VBA
body: <p>I have inherited an Access database from someone who built it about 20 years ago. Recently for some users out of the 90 who use it the function to create a Word document from templates that are populated by access has stopped working for some users. I have installed and reinstalled office so many times, I have made machines identical to each other and still one works and one doesn't. I'm hoping the below function will be enough for people who know a lot more about VBA than I to see something wrong. </p>
  
  <p>I have done some research and thought at first it would be missing references but can be 100% sure that it is not. All users are running access runtime 2000 and a mixture of office 2010 and office 365 the users who work are both on office 365 and Office 2010.</p>
  
  <p>Please see the below function and let me know if anything is glaringly obvious with the code, and let me know if more info is needed happy to supply it. </p>
  
  <p>Edit* The issue I currently face is where the user clicks on the "Create document" button and then it seemingly does nothing. It doesn't error, it doesn't create the document and doesn't open word full screen like it should. It does however move onto the next part where it asks them if they would like to log this in the logs. What I have noticed is that it appears to try and open Word, which I have seen using "Procmon" but nothing opens in the foreground.</p>
  
  <pre><code>    Public Sub PRINT_DOC(TEMPLAT As String)
  
      Dim oword As Word.Application
  
  '   Array bound increased from 20 to 100 [v1.1]
      Dim FIELDNAME(100) As String
      Dim strSQL As String
      Dim FILE_STR As String
      Dim dbs As Database
      Dim COUNTER As Integer
      Dim COUNTA As Integer
      On Error GoTo notloaded
  
      Dim theError As Integer
  
      Err.NUMBER = 0
      Set oword = New Word.Application
  
  notloaded:
      If Err.NUMBER = 429 Then
          Set oword = New Word.Application
          theError = Err.NUMBER
      End If
  
  On Error GoTo Err_PRINT_DOC
      DoCmd.Hourglass True
  
  
      oword.Documents.Add TEMPLAT
  
      oword.Visible = True
      oword.Activate
  
  
      COUNTER = oword.ActiveDocument.FormFields.COUNT
      For COUNTA = 1 To COUNTER
          FIELDNAME(COUNTA) = oword.ActiveDocument.FormFields(COUNTA).Name
      Next
  
      For COUNTA = 1 To COUNTER
          Select Case FIELDNAME(COUNTA)
              Case "CONTACT": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![CONTACT]
              Case "ADDRESS": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![FULL_ADDRESS]
              Case "REFERENCE": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![REFERENCE]
              Case "FAX": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![FAX_NO]
              Case "SUBJECT": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![JOBTITLE] &amp; " - " &amp; Forms![frm_correspondence]![SUBJECT]
              Case "JOB": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![JOBTITLE]
              Case "REPORT": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![SUBJECT]
              Case "DATE": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![DATE]
              Case "SIGNED": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![SIGNED]
              Case "FROM": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![FROM]
              Case "TO": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![TO]
              Case "CC": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![CC]
              Case "DEAR": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![DEAR]
  
              Case "INVOICE_SUM": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![INVOICE SUM]
              Case "INVOICE_VAT": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![INVOICE VAT]
              Case "INVOICE_NOTES": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![INVOICE NOTES]
               Case "INVOICE_NUMBER": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![INVOICE NO]
              Case "INVOICE_TOTAL": oword.ActiveDocument.FormFields(COUNTA).SELECT
                              oword.Selection.InsertBefore Forms![frm_correspondence]![INVOICE TOTAL]
          End Select
      Next
  
      oword.Visible = True
      oword.Activate
  
      If Forms![frm_correspondence]![COR PATH] &lt;&gt; "" And Not IsNull(Forms![frm_correspondence]![COR PATH]) Then
          FILE_STR = Forms![frm_correspondence]![COR PATH] &amp; "\" &amp; Forms![frm_correspondence]![FILE REF]
          oword.ActiveDocument.SaveAs FILENAME:=FILE_STR
      End If
      DoCmd.Hourglass False
      Set oword = Nothing
  
  Exit_PRINT_DOC:
      Exit Sub
  
  Err_PRINT_DOC:
      MsgBox Error$, vbExclamation, APP_NAME
      Resume Exit_PRINT_DOC
  End Sub 
  </code></pre>