Auf der Suche nach einem Excel bzw. VBA-Forum bin ich auf das nette Forum hier gestoßen.
Ich versuche zur Zeit Daten aus einem Excel-Dokument in ein Word-Dokument zu kopieren. Das ganze soll im Hintergrund passieren und durch ein Button in einem anderen Word-Dokument ausgelöst werden.
Mein aktueller Code im Word-Dokument lautet wie folgt: (Nur zum Starten)
Private Sub CommandButton4_Click()
Dim strFileName As String
Dim strFileNameExcel As String
Dim strTMP1 As String
Dim strTMP2 As String
Dim strTMP3 As String
Dim objWDApp As Object
Dim objDoc As Object
Dim objExcel As Object
On Error GoTo Fin
strFileName = "C:\" & TextBox1.Text & "\" & TextBox1.Text & "-00.docx"
strFileNameExcel = "C:\Test\" & TextBox1.Text & "\" & TextBox1.Text & ".xls"
If Dir(strFileNameExcel) "" Then
Application.ScreenUpdating = False
With Sheet1
strTMP1 = .Range("D2")
End With
On Error Resume Next
Set objWDApp = GetObject(, "Word.Application")
If objWDApp Is Nothing Then Set objWDApp = CreateObject("Word.Application")
On Error GoTo 0
With objWDApp
.Visible = True 'Or True, if Word is to be indicated
Set objDoc = .Documents.Open(strFileName)
Set objExcel = .Documents.Open(strFileNameExcel)
objDoc.ActiveDocument.Bookmarks("AuftragNr").Range = strTMP1
End With
MsgBox "Finished!"
Else
MsgBox "No file!"
End If
Fin:
Application.ScreenUpdating = True
' If Not objDoc Is Nothing Then objDoc.Save
' If Not objWDApp Is Nothing Then objWDApp.ActiveDocument.Close: objWDApp.Quit
Set objWDApp = Nothing
End Sub
Leider erhalte ich nur die Else-Antwort "No-file!".Vielleicht wäre einer von euch so nett und würde kurz mit einem geschulten VBA-Auge den Fehler sehen und mir helfen.
Vielen Dank vorab.
Gruß
Sam