Word - Fußzeile - Seiten - Datum - Autor!

Ein neues Worddokument soll erstellt bzw. ein vorhandenes Dokument geöffnet und in die Fußzeile folgendes eingegeben werden: "Seite ? von ?", "Datum" und "Autor" - erstellt und getestet in Excel 2010:

Option Explicit
Public Sub Test()
Dim objFooter As Object
Dim objWDApp As Object
Dim objWDDoc As Object
Dim objRange As Object
On Error GoTo Fin
Set objWDApp = OffApp("Word")
If Not objWDApp Is Nothing Then
'Set objWDDoc = objWDApp.Documents.Open("C:\Temp\Dok1.doc")
Set objWDDoc = objWDApp.Documents.Add
Set objFooter = objWDDoc.Sections(1).Footers(1)
With objFooter.Range
Set objRange = .Characters(Len(objFooter.Range.Text))
objFooter.Range.Text = "Seite "
Set objRange = .Characters(Len(objFooter.Range.Text))
objRange.Fields.Add objRange, -1, "PAGE"
Set objRange = .Characters(Len(objFooter.Range.Text))
objRange.Text = " von "
Set objRange = .Characters(Len(objFooter.Range.Text))
objRange.Fields.Add objRange, -1, "NUMPAGES"
Set objRange = .Characters(Len(objFooter.Range.Text))
objRange.Text = vbTab
Set objRange = .Characters(Len(objFooter.Range.Text))
objRange.InsertDateTime DateTimeFormat:="dd.MM.yyyy"
Set objRange = .Characters(Len(objFooter.Range.Text))
objRange.Text = vbTab
Set objRange = .Characters(Len(objFooter.Range.Text))
objRange.Fields.Add objRange, -1, "AUTHOR"
End With
End If
Fin:
Set objRange = Nothing
Set objFooter = Nothing
Set objWDDoc = Nothing
Set objWDApp = Nothing
If Err.Number <> 0 Then MsgBox "Fehler: " & _
Err.Number & " " & Err.Description
End Sub
Private Function OffApp(ByVal strApp As String) As Object
Dim objApp As Object
On Error Resume Next
Set objApp = GetObject(, strApp & ".Application")
Select Case Err.Number
Case 429
Err.Clear
Set objApp = CreateObject(strApp & ".Application")
objApp.Visible = True
If Err.Number > 0 Then
MsgBox Err.Number & " " & Err.Description
Set objApp = Nothing
End If
Case 0
Case Else
MsgBox Err.Number & " " & Err.Description
Set objApp = Nothing
End Select
On Error GoTo 0
Set OffApp = objApp
Set objApp = Nothing
End Function

Kommentare

Beliebte Posts aus diesem Blog

Formeln - auch Array - per VBA eintragen...

Alle Dateien eines Ordners - Optional mit Unterordner

Excel -> Word in Textmarken (Bookmarks)...