Search code examples
ms-accessms-wordvbaole

VBA code is getting error 462


I have the code below, in the OnClick event on a button that is located on a form. The code does the following:

In the sub form Forms! FrmFScomposition! PRODUCAO! [Prod_Cena_Guiao] (with an OLE object) I have a list of word documents, this list is updated with the help of a combobox. The code makes a Loop for all the documents and copies them to another sub form Forms! FrmFScomposition! SubfrmKitCenas! [FSKitCenasOLE], these documents are all in one. The code works fine, even if you repeat the process with the SAME data loaded. But when I choose another set of texts in the combobox, I get error 462, the first time I try the operation, but when I try again, the code works again. I'm tired of trying different possibilities, but I can not find a solution. Can someone help me or indicate something I'm missing? Below I will post the two subs that I am using. Thank you in advance for your attention.

code on button:

Private Sub Command54_Click()

  Call DoResetKit

  Dim FirstTime As Integer
  FirstTime = 1
  Me.FirstTimeBox = FirstTime

  Forms!frmFScomposicao!PRODUCAO.SetFocus
  DoCmd.RunCommand acCmdRecordsGoToFirst

  For f = 1 To Forms!frmFScomposicao!PRODUCAO![tiroliro]
    Call CompilarKitDiaGravacao
    DoCmd.RunCommand acCmdRecordsGoToNext
  Next f

  DoCmd.RunCommand acCmdRecordsGoToFirst
End Sub

Code on first UDF

Public Sub CompilarKitDiaGravacao()

  Dim CenasParaRecolha As Object
  Dim DocumentoDestino As Object

  Set CenasParaRecolha = Forms!frmFScomposicao!PRODUCAO![Prod_Cena_Guiao].Object.Application.WordBasic
  Forms!frmFScomposicao!PRODUCAO![Prod_Cena_Guiao].Action = acOLEActivate
  With CenasParaRecolha
    Selection.WholeStory
    Selection.Copy
  End With
  Set CenasParaRecolha = Nothing
  If Forms!frmFScomposicao.FirstTimeBox = 1 Then
    '  Forms!frmFScomposicao!subfrmKitCenas![FSKitCenasOLE].Action = acOLEPaste
    Set DocumentoDestino = Forms!frmFScomposicao!subfrmKitCenas![FSKitCenasOLE].Object.Application.WordBasic
    Forms!frmFScomposicao!subfrmKitCenas![FSKitCenasOLE].Action = acOLEActivate
    With DocumentoDestino
      'Selection.WholeStory
      'Selection.Delete
      Selection.EndKey wdStory
      Selection.InsertBreak Type:=wdSectionBreakContinuous
      Selection.PasteAndFormat wdPasteDefault
    End With
    Set DocumentoDestino = Nothing
    Forms!frmFScomposicao!FirstTimeBox = Forms!frmFScomposicao!FirstTimeBox + 1
  Else
    Set DocumentoDestino = Forms!frmFScomposicao!subfrmKitCenas![FSKitCenasOLE].Object.Application.WordBasic
    Forms!frmFScomposicao!subfrmKitCenas![FSKitCenasOLE].Action = acOLEActivate
    With DocumentoDestino
      Selection.EndKey wdStory
      Selection.InsertBreak                      'Type:=wdSectionBreakContinuous
      Selection.PasteAndFormat wdPasteDefault
    End With
    Set DocumentoDestino = Nothing
    Forms!frmFScomposicao!FirstTimeBox = Forms!frmFScomposicao!FirstTimeBox + 1
  End If

  'Set CenasParaRecolha = Nothing
  'Set DocumentoDestino = Nothing
End Sub

Code on second UDF

Public Sub DoResetKit()
  Dim ResetKit As Object

  Set ResetKit = Forms!frmFScomposicao!subfrmKitCenas![FSKitCenasOLE].Object.Application.WordBasic
  Forms!frmFScomposicao!subfrmKitCenas![FSKitCenasOLE].Action = acOLEActivate
  With ResetKit.Selection
    Selection.WholeStory
    Selection.Delete
  End With

  Set ResetKit = Nothing
End Sub

Solution

  • The working code for this is as follows:

    Button Code:

        Private Sub Command61_Click()
    Dim ServerWordFS As Object
    Set ServerWordFS = CreateObject("Word.Application")
    
    
    Dim FirstTime As Integer
    FirstTime = 1
    
    For LoopCenasKit = 1 To Forms!frmFScomposicao!subfrmFScenas![tiroliro]
    If FirstTime = 1 Then
    Me.FirstTimeBox = FirstTime
    Forms!frmFScomposicao!subfrmFScenas.SetFocus
    Forms!frmFScomposicao!subfrmFScenas![EQUIPA].SetFocus
    
    DoCmd.RunCommand acCmdRecordsGoToFirst
    
    
    Call StartKit
    
    DoCmd.RunCommand acCmdRecordsGoToNext
    FirstTime = FirstTime + 1
    Else
    
    Call AddKit
    
    DoCmd.RunCommand acCmdRecordsGoToNext
    FirstTime = FirstTime + 1
    End If
    Next LoopCenasKit
    
    ServerWordFS.Quit
    End Sub
    

    and two subs to create word doc wherever you want:

        Public Sub StartKit()
    
    Dim oAPP As Object
    Dim oDoc As Word.Document
    Dim cenaspararecolha As Object
    
            Set oAPP = CreateObject(Class:="Word.Application")
            With oAPP
            .Visible = True
    
            Set oDoc = .Documents.Add
            oDoc.SaveAs "C:\Fserv\FolhaServiço", wdFormatDocument
            End With
    
    Forms!frmFScomposicao!subfrmFScenas![Prod_Cena_Guiao].SetFocus
         Forms!frmFScomposicao!subfrmFScenas![Prod_Cena_Guiao].Action = acOLEActivate
        Set cenaspararecolha = Forms!frmFScomposicao!subfrmFScenas![Prod_Cena_Guiao].Object.Application.WordBasic
                With cenaspararecolha
                Selection.WholeStory
                Selection.Copy
                End With
    
    Forms!frmFScomposicao!subfrmFScenas![Prod_Cena_Guiao].Action = acOLEClose
    
    With oAPP
    .Selection.PasteSpecial DataType:=wdPasteRTF
    End With
    
    oDoc.Save
    oDoc.Activate
    oDoc.Close
    oAPP.Quit
    End Sub
    
    Public Sub AddKit()
    
    Dim oAPP As Object
    Dim oDoc As Word.Document
    Dim cenaspararecolha As Object
    
    
            Set oAPP = CreateObject(Class:="Word.Application")
            With oAPP
            .Documents.Open Filename:="C:\Fserv\FolhaServiço.doc"
            .Visible = True
            .Selection.EndKey wdStory
            .Selection.InsertBreak
            End With
    
    Forms!frmFScomposicao!subfrmFScenas![Prod_Cena_Guiao].SetFocus
         Forms!frmFScomposicao!subfrmFScenas![Prod_Cena_Guiao].Action = acOLEActivate
        Set cenaspararecolha = Forms!frmFScomposicao!subfrmFScenas![Prod_Cena_Guiao].Object.Application.WordBasic
                With cenaspararecolha
                Selection.WholeStory
                Selection.Copy
                End With
    Forms!frmFScomposicao!subfrmFScenas![Prod_Cena_Guiao].Action = acOLEClose
    
    With oAPP
    .Selection.PasteSpecial DataType:=wdPasteRTF
    Set oDoc = .ActiveDocument
    oDoc.Save
    End With
    
    oDoc.Close
    oAPP.Quit
    End Sub