Hallo,
ich bin VBA-Anfänger und sitze nun schon seit gestern an einem Problem, für das ich die Lösung einfach nicht finde.
Ich möchte aus Excel ein Worddokument öffnen, um dann später auf Inhalte zugreifen zu können. Folgenden Code habe ich zusammengebastelt (s.u.).
Er öffnet auch die Worddatei, gibt mit als Anzahl = 1 zurück. Soweit so gut, aber dann kommt die Fehlermeldung: Dieser Befehl ist nicht verfügbar, weil kein Dokument geöffnet ist. Gelb markiert wird das hier:
Set AppWord = ActiveDocument
Das verstehe ich einfach nicht...
Option Explicit
Public Sub Test ()
Dim sPfad As String
Dim appWord As Object
Dim AppWD As Word.Document
Dim Anzahl As Single
Set appWord = CreateObject("Word.Application") 'Word öffnen
sPfad = "D:\VBA\VBA2.docx" ' Pfad ändern für Tests
appWord.Visible = True 'Word sichtbar machen
appWord.Documents.Open sPfad
AppActivate "VBA2.docx"
Anzahl = appWord.Documents.Count
MsgBox (Anzahl)
If appWord.Documents.Count > 0 Then
Set AppWord = ActiveDocument
Debug.Print appWord.Name
Else
Debug.Print "nix"
End If
Set appWord = Nothing
MsgBox "juchuh"
End Sub
Vielen Dank für eure Hilfe!
Eva
|