2016-01-20 153 views
1

我目前有個問題,每次我試圖通過vba/excel打開一個word文檔時,得到一個應用程序/對象錯誤。我的想法是,我試圖比較兩個表中的數據並刪除不好的結果。之後,我想插入整個表到現有的文檔文件,即時選擇從選擇/打開窗口中選擇。使用VBA實現Excel數據到現有的Word文檔

我的代碼

Private Sub CommandButton1_Click() 

Dim varDatei As Variant 
Dim wordDatei As Variant 
Dim objExcel As New Excel.Application 
Dim objSheet As Object 
Dim wordDoc As Object 
Dim extBereich As Variant 
Dim intBereich As Variant 

Dim appWord As Object 

Set intBereich = ThisWorkbook.Sheets(1).Range("A4:A11") 

Dim loopStr As Variant 
Dim loopStr2 As Variant 
Dim found() As Variant 
Dim loopInt As Integer 
Dim endStr As Variant 

Dim extBereich2 As Variant 

loopInt = 1 
varDatei = Application.GetOpenFilename("Excel-Dateien (*.xlsx), *.xlsx") 

If varDatei <> False Then 
    objExcel.Workbooks.Open varDatei 
    Set objSheets = objExcel.Sheets(1) 
    objSheets.Activate 
    LetzteZeile = objSheets.Cells(objSheets.Rows.Count, 3).End(xlUp).Row 
    Set extBereich = objSheets.Range("B3:B" & LetzteZeile) 

    ReDim found(1 To LetzteZeile) 
    For Each loopStr In extBereich 
     objSheets.Range("F" & loopStr.Row) = "Good" 
     objSheets.Cells(loopStr.Row, 6).Interior.ColorIndex = 4 
     For Each loopStr2 In intBereich 
      If (StrComp(loopStr, loopStr2, vbBinaryCompare) = 0) = True Then 
       found(loopInt) = objSheets.Range("A" & loopStr.Row) 
       loopInt = loopInt + 1 
       objSheets.Cells(loopStr.Row, 6) = "Bad" 
       objSheets.Cells(loopStr.Row, 6).Interior.ColorIndex = 3 
       Exit For 
      End If 
     Next loopStr2 
    Next loopStr 
    loopStr = "" 
    If (loopInt <> 1) Then 
     endStr = "This is bad:" & vbLf 
     For Each loopStr In found 
     If (Trim(loopStr & vbNullString) <> vbNullString) Then 
      endStr = endStr & loopStr & vbLf 
     End If 
     Next loopStr 
     MsgBox (endStr) 
    Else 
     MsgBox ("Everythings good") 
    End If 
    Set appWord = CreateObject("Word.Application") 
    appWord.DisplayAlerts = False 
    Debug.Print ("123") 
    Set wordDoc = appWord.Documents.Open(Application.GetOpenFilename("Word-Dateien (*.doc;*.docx;),*.doc;*.docx")) 
    wordDoc.Activate 
    Debug.Print ("456") 
    loopStr = "" 
    For Each loopStr In extBereich 
     If (objSheets.Cells(loopStr.Row, 6).Interior.ColorIndex = 3) Then 
      objSheets.Range("A" & loopStr.Row & ":" & "E" & loopStr.Row).Delete 
     End If 
    Next loopStr 
    objSheets.Range(Columns(2), Columns(4)).Delete 
    objSheets.Range("A3:B" & LetzteZeile).Copy 
    appWord.Documents(1).Range.Paste 
    With appWord.Documents(1).Tables(1) 
     .Columns.AutoFit 
    End With 
    appWord.PrintOut 
    objExcel.Quit 
    appWord.Quit 
    Set appWord = Nothing 
    Set objExcel = Nothing 
    Debug.Print loopInt 


Else 
    MsgBox "Error" 
End If 

End Sub 

也許你知道某人的什麼問題?

錯誤代碼是1004 - 應用程序或對象錯誤

與問候,並感謝回答

+0

有什麼關係C++和VB.NET? – mikedu95

+0

對不起,剛剛autoclicked的標籤:( – ndslr

+0

什麼行發生錯誤? – Gareth

回答

1

你的問題是與線:

objSheets.Range(Columns(2), Columns(4)).Delete 

你需要指定的列是,例如

objSheets.Range(objSheets.Columns(2), objSheets.Columns(4)).Delete 

+0

只是....哇 非常感謝:) – ndslr

+1

沒問題!我強烈建議您使用斷點(左鍵單擊代碼旁邊的灰色條)以查找類似這樣的錯誤。把一個放在你知道工作的最後一行,然後和F8一起,直到遇到錯誤。 – EamonnT

相關問題