简体   繁体   中英

Run time error 5981 when trying to create MS Word doc files from Access

I have created an MS word document which is aimed at downloading field values from a table (query) and creating a report for each record within the table and saving it in a specified directory. Unfortunately, after trying everything, i continue to get the error 5981 (Impossibile to open the memory macro). I am almost positive that all of the code is fine. Below is my code. Any help will be greatly appreciated. Thanks

Public Sub LetteraPromossi()


  Dim rstStudentiPromossi As New ADODB.Recordset
  Dim appWord As New Word.Application
  Dim strPercorso As String
  strPercorso = Application.CurrentProject.Path & "\"
  Const conPercorso As String = "C:\Users\Andrew\Documents\Andrea Lenti\MS Access\eserciziVBAAccess2010\"

  DoCmd.SetWarnings (False)
  'Esegui le query di aggiornamento che sostituiscono i campi Null con ""

  DoCmd.OpenQuery "qryEliminaNulliDaIndirizzo"
  DoCmd.OpenQuery "qryEliminaNulliDaCittà"
  DoCmd.OpenQuery "qryEliminaNulliDaCAP"
  DoCmd.OpenQuery "qryEliminaNulliDaProvincia"

  DoCmd.SetWarnings (True)


  rstStudentiPromossi.Open "qryStudentiPromossi", _
                            CurrentProject.Connection, adOpenForwardOnly

  Do Until rstStudentiPromossi.EOF

      With appWord
        .Documents.Add strPercorso & "comunicazioni.dotx"
        .Selection.Goto wdGoToBookmark, Name:="studente"
        .Selection.TypeText rstStudentiPromossi!Nome & _
        " " & rstStudentiPromossi!Cognome
        .Selection.Goto wdGoToBookmark, Name:="Indirizzo"
        .Selection.TypeText rstStudentiPromossi!Indirizzo
        .Selection.Goto wdGoToBookmark, Name:="Città"
        .Selection.TypeText rstStudentiPromossi!Città
        .Selection.Goto wdGoToBookmark, Name:="CAP"
        .Selection.TypeText rstStudentiPromossi!CAP
        .Selection.Goto wdGoToBookmark, Name:="provincia"
        .Selection.TypeText rstStudentiPromossi!Provincia
        .Selection.Goto wdGoToBookmark, Name:="Media"
        .Selection.TypeText rstStudentiPromossi!Media
        .Visible = True
      End With
      rstStudentiPromossi.MoveNext
  Loop
  rstStudentiPromossi.Close
  Set rstStudentiPromossi = Nothing
End Sub 

It doesn't look like you ever save or close the Word documents -- 1 for every record in the recordset. There is a limit to how many documents you can have open at the same time before running out of memory. I can't tell how many records you are dealing with, but you'll probably want to save and close them within the loop to free up memory for the next one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM