VBA 代码出现错误 462
VBA code is getting error 462
我有下面的代码,在位于窗体上的按钮上的 OnClick 事件中。该代码执行以下操作:
在子表单Forms中! FrmFS组合!生产! [Prod_Cena_Guiao](带有 OLE 对象)我有一个 word 文档列表,这个列表是在组合框的帮助下更新的。该代码为所有文档创建一个循环并将它们复制到另一个子表单 Forms! FrmFS组合! SubfrmKitCenas! [FSKitCenasOLE],这些文件都在一个。代码工作正常,即使您在加载相同数据的情况下重复该过程也是如此。但是当我在组合框中选择另一组文本时,第一次尝试该操作时出现错误 462,但是当我再次尝试时,代码再次运行。我厌倦了尝试不同的可能性,但我找不到解决方案。有人可以帮助我或指出我缺少的东西吗?下面我将 post 我正在使用的两个潜艇。预先感谢您的关注。
按钮上的代码:
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
其工作代码如下:
按钮代码:
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
还有两个 subs 可以在任何你想要的地方创建 word 文档:
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
我有下面的代码,在位于窗体上的按钮上的 OnClick 事件中。该代码执行以下操作:
在子表单Forms中! FrmFS组合!生产! [Prod_Cena_Guiao](带有 OLE 对象)我有一个 word 文档列表,这个列表是在组合框的帮助下更新的。该代码为所有文档创建一个循环并将它们复制到另一个子表单 Forms! FrmFS组合! SubfrmKitCenas! [FSKitCenasOLE],这些文件都在一个。代码工作正常,即使您在加载相同数据的情况下重复该过程也是如此。但是当我在组合框中选择另一组文本时,第一次尝试该操作时出现错误 462,但是当我再次尝试时,代码再次运行。我厌倦了尝试不同的可能性,但我找不到解决方案。有人可以帮助我或指出我缺少的东西吗?下面我将 post 我正在使用的两个潜艇。预先感谢您的关注。
按钮上的代码:
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
其工作代码如下:
按钮代码:
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
还有两个 subs 可以在任何你想要的地方创建 word 文档:
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