2017-03-10 62 views
0

我有下面的代碼,位於窗體上的按鈕上的OnClick事件。該代碼執行以下操作:VBA代碼出錯462

在子窗體Forms! FrmFScomposition! PRODUCAO! [Prod_Cena_Guiao](帶有一個OLE對象)我有一個單詞文檔列表,這個列表是在一個組合框的幫助下更新的。該代碼爲所有文檔創建一個Loop並將它們複製到另一個子窗體Forms! 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 
+0

隨着ResetKit.Selection Selection.WholeStory ------這是該行強調,但如果我重新運行,代碼作品agaiñ Selection.Delete End With – Armando

+0

您的'With'塊沒有使用捕獲的引用。 – Comintern

+0

@Comintern試圖告訴你什麼'帶有ResetKit.Selection Selection.WholeStory Selection.Delete End With'是不必要的。你的兩行內容應該是'.WholeStory'和'.Delete' – FreeMan

回答

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文檔,無論你想:

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