613 Aufrufe
Gefragt in Anwendungen(Java,C++...) von
Hallo Zusammen,

Ich habe ein kleines Problem und komme einfach nicht weiter.
Zur Zeit würde ich gerne eine Datenbank in Excel erstellen welche ihre Daten aus einer Worddatei rauszieht.
Das Makro schreibe ich wie folgt:

Sub Datenbank()

Dim vntPathAndFileNames As Variant
Dim strPathAndFile As String
Dim lngI As Long
Dim lngLetzteZeileZiel As Long
Dim lngLetzteZeileQuelle As Long

Dim wb As Workbook
Dim ws As Worksheet
Dim w As Application
Dim d As Object
Dim i As Long
Dim objcc As Control

Set wb = Workbooks("Agenda erstellen mit Word.xlsm")
Set ws = wb.Worksheets("Word")



vntPathAndFileNames = Application.GetOpenFilename( _
FileFilter:="Word Files (*.doc*), *.doc*", _
Title:="Änderungsanträge ", _
MultiSelect:=True)
If VarType(vntPathAndFileNames) = vbBoolean Then
MsgBox "Abgebrochen!"
Else
' For lngI = LBound(vntPathAndFileNames) To UBound(vntPathAndFileNames)
' strPathAndFile = vntPathAndFileNames(lngI)
On Error Resume Next
Set w = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set w = CreateObject("Word.Application")
Err.Clear
End If
Set d = w.documents.Open(strPathAndFile)


ws.Cells(2, 3).Value = d.FormFields("SG").Object.Value


d.Close False
DoEvents
' Set d = Nothing
w.Quit
' Set w = Nothing
' Set ws = Nothing
'Next
End If
End Sub

Da Problem hiebei ist, die Word Vorlage benutzt noch alte Formularsteuerelemente (Textfelder und Checkboxen).
Am Anfang sollen die Daten über eine Maske eingelesen werden, also mehrere Dateien auf einmal. Passt soweit auch.
Aber der Übertrag funktioniert noch nicht richtig.
Ich hatte das Word-Dokument auch in Excel nachgebaut um mit den OLEObjects zu arbeiten, was funktioniert hat. Also den Code dafür schreiben kann ich. Nur komme ich einfach nicht auf die alten Formularsteuerelemente.

Gruß,
Chris

1 Antwort

0 Punkte
Beantwortet von kicia Mitglied (939 Punkte)
Vielleicht sind die Formularelemente ContentControls und nicht FormFields ?

Das kannst Du so herausfinden:

Debug.Print "FormFields: " & doc.FormFields.Count
Debug.Print "ContentControls: " & doc.ContentControls.Count

Wenn es ContentControls sind, kannst Du so darauf zugreifen:

Dim ctc As Word.ContentControl
set ctc = doc.SelectContentControlsByTitle("testXX")(1)

(SelectContentControlsByTitle liefert eine Liste zurück, daher der index (1). Es kann theoretisch mehrere ContentControls mit dem selben Titel geben.)
...