尝试从Access创建MS Word doc文件时运行时错误5981

时间:2014-02-01 13:55:26

标签: access-vba ms-access-2010

我创建了一个MS word文档,旨在从表(查询)中下载字段值,并为表中的每条记录创建一个报告,并将其保存在指定的目录中。 不幸的是,在尝试了一切后,我继续得到错误5981(Impossibile打开内存宏)。我几乎肯定所有的代码都没问题。以下是我的代码。任何帮助将不胜感激。感谢

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 

1 个答案:

答案 0 :(得分:1)

看起来你似乎没有保存或关闭Word文档 - 记录集中的每条记录都是1。在内存不足之前,可以同时打开多少文档是有限制的。我不知道你正在处理多少条记录,但你可能想要在循环中保存和关闭它们以释放下一个记忆。