intermittent Access to Word "Copy & paste" anomaly

Happy YN

Registered User.
Local time
Today, 01:29
Joined
Jan 27, 2002
Messages
425
I have a procedure running in MSaccess which creates a multi page word document based on records in a database. Simplified it runs like this:-

open recordset
For each record:
open a presaved word document(1) which contains bookmarks based on fields in each record
populate the document with the contents of each field
select all and copy
open new document(2) and paste
close original document(1) with bookmarks WITHOUT saving
insert page break into document(2)
go to next record
close recordset

Thus for an eleven record recordset, a document is created with eleven pages.

All is working fine but recently We are getting the following weird scenario

If we create an 11 page doc as above and then leave it open in Word. Then go back to Access and do the run again without changing anything else. The second or sometimes third time the run is finished, a 7 page document has been created with some of the records superimposed on others haphazardly. Even worse the previous open document of eleven pages has now swollen to 21 pages (with the new ones being blank) merely because it was open.

I don't know if this is a vb issue or a Word issue. I have tried it on more than one system and we have the same problems.

If necessary I can post the code

Even if you are reading this and have no answer, any light shed on the mystery would be appreciated
Thanks
 
Sounds like a memory error, but possibly a reference or focus issue. Try pasting your code.
 
Thanks for the quick response!
I am posting my code for the whole procedure.
The first part builds a sql for the recordset. i have tested the generated sql and it is fine. Please ignore the part which are irrelevant
Thanks again
Code:
Private Sub Command2_Click()
   Dim rs As ADODB.Recordset, oBookMark As Object, dbConn As ADODB.Connection, strBMName As String
   Dim oWord As Word.Application, oDocMain As Word.Document, oDocTemp As Word.Document
   Dim strTmp As String, strTemplateFileName As String, intVNumber As Long, blnIsOpen As Boolean
   Dim intError As Integer
   Dim docfilevI As String, docfilepI As String, docfilevP As String, docfilepP As String
   If Not Val(Me.txtStartNo) > 0 Then
      MsgBox "You have not filled in a starting number for the " & _
        "vouchers", vbExclamation + vbOKOnly + vbDefaultButton1, _
        "Missing Information"
      Exit Sub
   Else
      appPath = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
      docfilevI = appPath & "Config\Voucher mosad standard.doc" 'With voucher
      docfilepI = appPath & "Config\Blank mosad standard.doc" 'Without voucher
      docfilevP = appPath & "Config\Voucher cause standard.doc" 'With voucher
      docfilepP = appPath & "Config\Blank cause standard.doc" 'Without voucher
      intVNumber = Me.txtStartNo
      Set rs = New ADODB.Recordset
      Set dbConn = CurrentProject.Connection
       Dim MyString As String
       Dim myStringNew As String
       
Open "c:\temp\dg\temps.tmp" For Input As #1    ' Open file for input.
Do While Not EOF(1)    ' Loop until end of file.
    Input #1, MyString    ' Read data into variable
    Debug.Print MyString    ' Print data to the Immediate window.
Loop
Close #1    ' Close file.
 MyString = Replace(MyString, "%", "")
 'MyString = Replace(MyString, Chr(34) & "%", "*")
 
 If InStr(MyString, ProfileGetItem("report8", "table", "nothing", appPath & "config\control.ini")) < 1 Then
 MsgBox "Please return to the search interface and select " & _
    "the correct view before selecting reports again", _
    vbInformation + vbOKOnly + vbDefaultButton1, "Missing information"
 Exit Sub
 End If
 
 myStringNew = Left(MyString, InStr(MyString, "FROM") - 1)
 myStringNew = myStringNew & "," & ProfileGetItem("report8", "select", "nothing", appPath & "config\control.ini")
myStringNew = myStringNew & " FROM " & ProfileGetItem("report8", "table", "nothing", appPath & "config\control.ini")
If InStr(MyString, "WHERE") < 1 Then
myStringNew = myStringNew & " WHERE " & ProfileGetItem("report8", "where", "", appPath & "config\control.ini")
Else
Dim strWhere As String
strWhere = Right(MyString, Len(MyString) - Len(Left(MyString, InStr(MyString, "WHERE") - 1)))
strWhere = Left(strWhere, InStr(strWhere, "ORDER BY") - 1)
'myStringNew = myStringNew & " " & strWhere
myStringNew = myStringNew & " " & strWhere & " AND " & ProfileGetItem("report8", "where", "", appPath & "config\control.ini")

    End If
    
   myStringNew = myStringNew & " ORDER BY " & ProfileGetItem("report8", "orderby", "nothing", appPath & "config\control.ini")
   
    MyString = myStringNew
    
    Debug.Print MyString    ' Print data to the Immediate window.

      'rs.Open "getRemmitance", dbConn, adOpenStatic, adLockPessimistic
      rs.Open MyString, dbConn, adOpenStatic, adLockPessimistic
      rs.Filter = "locked <>" & 1
      rs.Requery

      If rs.RecordCount = 0 Then
      MsgBox "There are no remittance slips to process. " & _
        "Check that those unlocked are within date.", _
        vbInformation + vbOKOnly + vbDefaultButton1, "No records found!"
       DoCmd.Close acForm, "frmremmitance", acSaveYes
      Exit Sub
      End If
      rs.Filter = ""
      rs.Requery
      
           rs.Filter = "mpay='Voucher' And Locked <>" & 1
 
      rs.Requery
      strTmp = rs.RecordCount & " Vouchers to be printed" & vbCr
      'rs.Filter = ""
      rs.Filter = "mpay<>'Voucher' And mpay<>'' And mpay<> null and locked <>" & 1
      rs.Requery
      strTmp = strTmp & rs.RecordCount & " other notes to be printed" & vbCr & _
        "Please insert correct number of required stationery"
      MsgBox (strTmp)
      'rs.Filter = ""
      rs.Filter = "locked <>" & 1
      rs.Requery
      Me.cdlSaveAs.CancelError = True
      On Error GoTo docErrorHandle:
      Me.cdlSaveAs.ShowSave
      On Error GoTo 0
      rs.MoveFirst
      Set oWord = StartWord(blnIsOpen)
      Set oDocMain = oWord.Documents.Add
      Do While Not rs.EOF
      
         If rs("mpay") = "Voucher" Then
         If Len(rs("ijname")) > 0 Then
            strTemplateFileName = docfilevI
            Else
            strTemplateFileName = docfilevP
            End If
            If Me.chkTest = 0 Then
            rs("cnum") = intVNumber
            rs.Update
            End If
            intVNumber = intVNumber + 1
            
         Else
         If Len(rs("ijname")) > 0 Then
            strTemplateFileName = docfilepI
            Else
            strTemplateFileName = docfilepP
            End If
            
         End If
         Debug.Print strTemplateFileName
         If Not IsNull(rs("greeting")) Then
            If rs("greeting") <> "" Then strTemplateFileName = rs("greeting")
         End If
         Debug.Print rs("id")
         Debug.Print rs("greeting")
         On Error GoTo docErrorHandle:
         Set oDocTemp = oWord.Documents.Open(strTemplateFileName, , True)
         On Error GoTo 0
         oDocTemp.Application.Visible = True
         For Each oBookMark In oDocTemp.Bookmarks
            If ((Val(Right(oBookMark.Name, 1)) > 0) And (Mid(oBookMark.Name, Len(oBookMark.Name) - 1, 1) = "_")) Then
               strBMName = Left(oBookMark.Name, Len(oBookMark.Name) - 2)
            Else
               strBMName = oBookMark.Name
            End If
            If Left(strBMName, 5) <> "TOTXT" Then
               If IsNull(rs(strBMName).Value) Then
                  oBookMark.Range.Text = ""
               Else
                  oBookMark.Range.Text = rs(strBMName).Value
               End If
            Else
               If IsNull(rs(Mid(strBMName, 6)).Value) Then
                  oBookMark.Range.Text = ""
               Else
                  oBookMark.Range.Text = SpellNumber(rs(Mid(strBMName, 6)).Value)
               End If
            End If
         Next
         'oDocTemp.Activate
         oDocTemp.Application.Selection.WholeStory
         oDocTemp.Application.Selection.Copy
         oDocMain.Activate
         oDocMain.Application.Selection.PasteAndFormat (wdPasteDefault)
         oDocMain.SaveAs cdlSaveAs.FileName
        'this locks only vouchers
         'If rs("mpay") = "Voucher" Then rs("locked") = 1
         Me.txtStartNo = intVNumber
 
        'this locks all printed matter if test checkbox is unticked
                If Me.chkTest = 0 Then

        rs("locked") = 1
        'save the voucher template
        rs("greeting") = strTemplateFileName
         rs.Update
         Else
         End If
         'oDocMain.Application.Selection.InsertBreak Type:=wdPageBreak
         oDocTemp.Close (False)
endofLoop:
         rs.MoveNext
         If Not rs.EOF Then oDocMain.Application.Selection.InsertBreak Type:=wdPageBreak
      Loop
      Me.ActiveControl.SetFocus
      If intError > 0 Then MsgBox intError & " documents failed-Please check file source exists and is correctly named!", vbCritical
      'If MsgBox("do you want to print this Document?", vbYesNo) = vbYes Then oDocMain.PrintOut
      'If blnIsOpen Then
      '   oDocMain.Close
      'Else
      '   oDocMain.Application.Quit
      'End If
   End If
        rs.Close
        
         Set rs = Nothing
 
          
          DoCmd.Close

   Exit Sub
docErrorHandle:
   Debug.Print "Error: "; Err.Number
   Select Case Err.Number
      Case 5174 'Template file not found
      Case 5273
         intError = intError + 1
         Err.Clear
         Resume endofLoop:
      Case cdlCancel 'Cancel pressed
         Exit Sub
   End Select
   Resume Next
   
   'Close Form
'DoCmd.Close

End Sub
 
I have a procedure running in MSaccess which creates a multi page word document based on records in a database.
Forgive me for asking silly questions, but... Isn't this what a mail-merge does?
Was that not an option?

Can you paste the template onto a report and then add textboxes for the values?
 
If you mean a mail-merge in access, this was not an option since the user wants to be able to edit the individual pages and resave them for later use.

If you mean a mail merge in word, Firstly it will entail the recordsource being open each time one wants to view the document and to copy and paste it would bring about the same problems.
 

Users who are viewing this thread

Back
Top Bottom