ich habe folgendes Problem. Ich möchte aus Word eine Excel-Datei befüllen. Funktioniert, falls die Datei geöffnet ist (auch wenn mehrere Dateien geöffnet sind. Kann mir jemand die Schleifen richten.Fehlermeldung "Bitte Excel starten und gewünschtes Dokument öffnen!"
Danke SR
Function ExcelApplication() As Excel.Application
Dim appExcel As Excel.Application
' Voraussetzung: Excel ist als Verweis eingebunden
On Error GoTo 0 ' Setzt Fehlerspeicher auf 0 zurück
On Error Resume Next ' Falls ein Fehler auftritt, bitte weiter machen
' Achtung bei GetObject: Anwendung ist zweiter Parameter, erster bleibt leer!
Set appExcel = GetObject(, "Excel.Application")
If Err.Number 0 Then ' Wenn Fehlernummer ungleich 0, dann ...
MsgBox "Bitte Excel starten und gewünschtes Dokument öffnen!"
On Error GoTo 0 ' Schaltet 'On Error Resume Next' aus
Exit Function
End If
On Error GoTo 0 ' Schaltet 'On Error Resume Next' aus
If appExcel.Visible = False Then
MsgBox "Bitte Excel starten bzw. prüfen, ob Excel z. B. durch Bearbeitung einer Zelle gesperrt ist!"
Exit Function
End If
Set ExcelApplication = appExcel
' appExcel.Worksheets.Item = "Verbindung hergestellt!"
End Function
Public Sub OpenExcel()
Dim strFile As String
Dim ObjExc As Excel.Worksheet
Dim lngIndex As Long
Dim lngDatensatz As Long
Dim myData As tpeDocData
Dim lngRow As Long
strFile = modWordExcel.BrowseToFileExcel
If strFile = "" Then Exit Sub
For lngIndex = 1 To ExcelApplication.Workbooks.Count
If ExcelApplication.Workbooks.Item(1).FullName = strFile Then
ExcelApplication.Workbooks.Item(1).Activate
If ExcelApplication.Workbooks.Item(1).FullName strFile Then
ExcelApplication.Workbooks.Open strFile
End If
End If
Next lngIndex
Set ObjExc = ExcelApplication.ActiveSheet
Dialogexecut
' Daten aus Excel einlesen (ReadData)
lngDatensatz = ExcelApplication.Selection.Row
myData = ReadDataset(lngDatensatz)
' Daten in Word eintragen (WriteData)
WriteDocumentData myData, lngRow
Application.Activate
End Sub