Ich habe ein Excel-Arbeitsblatt, von dem ich versuche, Informationen in ein Wordfile "Template" (nur ein Word-Dokument im Layout, das ich will), die Lesezeichen enthält einfügen. Was würde Ich mag zu tun ist:Kopieren und Einfügen EINSCHLIESSEN Lesezeichen VBA
- Kopieren Sie alles im Word-Dokument (einschließlich Lesezeichen)
- Ersetzen Sie die Lesezeichen mit den Daten in meinem Blatt
- Zum Ende der Seite, legen Sie eine Seite brechen und den kopierten Text einfügen, einschließlich Lesezeichen
- Schleife durch die Punkte 2 & 3 für alle Zeilen in meiner Excel-Datei
ich einige Code zusammen geflickt haben, aber ich bin nicht in der Lage, das Buch zu bekommen Markieren Sie, um den Text mit den noch intakten Lesezeichen einzufügen. Kann jemand von euch mir helfen, dorthin zu gelangen?
Sub ReplaceBookmarks
'Select template
PickFolder = "C:\Users\Folder"
Set fdn = Application.FileDialog(msoFileDialogFilePicker)
With fdn
.AllowMultiSelect = False
.Title = "Please select the file containing the Template"
.Filters.Clear
.InitialFileName = PickFolder
If .Show = True Then
Temp = fdn.SelectedItems(1)
End If
End With
'open the word document
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Open(Temp)
'show the word document - put outside of loop for speed later
wdApp.Visible = True
'Copy everything in word document
wdDoc.Application.Selection.Wholestory
wdDoc.Application.Selection.Copy
LastRow2 = 110 ' In real code this is counted on the sheet
For i = 2 To LastRow2
'Data that will replace bookmarks in ws2 (defined somewhere in real code)
Rf1 = ws2.Cells(i, 4).Value
Rf2 = ws2.Cells(i, 2).Value
Rf3 = ws2.Cells(i, 3).Value
'replace the bookmarks with the variables - references sub "Fillbookmark"
FillBookmark wdDoc, Rf1, "Rf1"
FillBookmark wdDoc, Rf2, "Rf2"
FillBookmark wdDoc, Rf3, "Rf3"
' Jump to bottom of document, add page break and paste
With wdDoc
.Application.Selection.EndKey Unit:=wdStory
.Application.Selection.InsertBreak Type:=wdPageBreak
.Application.Selection.PasteAndFormat (wdFormatOriginalFormatting)
End With
Next i
End Sub
Sub FillBookmark(ByRef wdDoc As Object, _
ByVal vValue As Variant, _
ByVal sBmName As String, _
Optional sFormat As String)
Dim wdRng As Object
'store the bookmarks range
Set wdRng = wdDoc.Bookmarks(sBmName).Range
'if the optional format wasn’t supplied
If Len(sFormat) = 0 Then
'replace the bookmark text
wdRng.Text = vValue
Else
'replace the bookmark text with formatted text
wdRng.Text = Format(vValue, sFormat)
End If
End Sub
Das war super aufschlussreich, danke !!! Es ging wirklich nur darum, die Lesezeichen zu löschen, nachdem ich alles kopiert hatte. Dies war das erste Mal, dass ich mit Lesezeichen arbeiten musste, so dass mir die Informationen wirklich viel Arbeit erspart haben. Du bist der Boss @ Maarten van Stam –