VBA代码收到错误462

时间:2017-03-10 18:47:18

标签: vba ms-word access-vba ole

我在下面的代码中,在OnClick事件中位于表单上的按钮上。代码执行以下操作:

在子表格中! FrmFScomposition! PRODUCAO! [Prod_Cena_Guiao](带有OLE对象)我有一个word文档列表,这个列表在组合框的帮助下更新。代码为所有文档创建一个循环并将它们复制到另一个子表单窗体! FrmFScomposition! SubfrmKitCenas! [FSKitCenasOLE],这些文件都在一个。即使您在加载了SAME数据的情况下重复该过程,代码也能正常工作。但是当我在组合框中选择另一组文本时,我第一次尝试操作时得到错误462,但是当我再次尝试时,代码再次起作用。我厌倦了尝试不同的可能性,但我找不到解决方案。有人可以帮助我或指出我失踪的东西吗?下面我将发布我正在使用的两个潜艇。提前感谢您的关注。

按钮上的

代码:

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

第一个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

第二个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

1 个答案:

答案 0 :(得分:0)

这方面的工作代码如下:

按钮代码:

    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

和两个子目录,随时随地创建word doc:

    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