I'm sorry for not using the code tags, I will from now on.
I was trying to keep the code simple as not to confuse you with other code. My code with which I open a word document is below. The Word document has a set template (.dot) that it uses, which I sadly enough can not change or open. I fill the bookmarks in this document with Excel, but the letters (Word-documents) are protected. I could fill in the date (on the second page) with my document, but if the letter is printed a few days later and the date on the first page is altered the date on the second page in the shape/table in the header on the second page does not change automatically. The StyleRef Field is new to me, is this very different from the Ref Field? Maybe that will work.
I try to simulate a Word document that looks alike with a shape and a table in the header and try my code to see if it works.
By the way, I prefer not selecting anything like I do in Excel but I am not so familiar with macro's in Word.
Your code I have tried, but it does not work because it gives an error, see code below (Set wdRng):
Code:
With objDoc
Dim wdRng As objWord.Range, StrBkMkNm As String
StrBkMkNm = "MySpots"
Set wdRng = .Bookmarks("MySpots").Range 'It gives an error on this line
'Insert header (bookmark MySpot is in Header, Bookmark Myspots is in table in header, Bookmark MyReference is in normal text)
.Fields.Add Range:=.Bookmarks(StrBkMkNm).Range, Type:=wdFieldEmpty, Text:="REF MyReference \*Charformat", PreserveFormatting:=False
.Bookmarks.Add Range:=wdRng, Name:=StrBkMkNm
End With
I was trying your code, and altered it:
Code:
'Next line works:
.bookmarks("BMshapeHeader").Range.Text = "Hello!"
'Next lines do NOT work:
.Fields.Add Range:=.bookmarks("BMshapeHeader").Range, Type:=wdFieldEmpty, Text:="REF MyReference \*Charformat", PreserveFormatting:=False
.Fields.Add Range:=.Sections(1).Headers(1).Shapes(1).TextFrame .Textrange.bookmarks("BMshapeHeader").Range, Type:=wdFieldEmpty, Text:="REF MyReference \*Charformat", PreserveFormatting:=False
.Fields.Add Range:=.Sections(1).Headers(1).tables(1).bookmarks ("BMtableHeader").Range, Type:=wdFieldEmpty, Text:="REF MyReference \*Charformat", PreserveFormatting:=False
.Sections(1).Headers(1).tables(1).Fields.Add Range:=.bookmarks("BMtableHeader").Range, Type:=wdFieldEmpty, Text:="REF MyReference \*Charformat", PreserveFormatting:=False
.Sections(1).Headers(1).Shapes(1).TextFrame.Textra nge.bookmarks("BMshapeHeader").Fields.Add Range:=.bookmarks("BMshapeHeader").Range, Type:=wdFieldEmpty, Text:="REF MyReference \*Charformat", PreserveFormatting:=False
My code that I use to open and fill out the bookmarks:
Code:
Sub CreateWordDoc(Optional Dummy As String)
Dim wdApp As Object
Dim wdDoc As Object
Dim wdFormField As Object
Dim rCell As Range
Dim rRng As Range, rngName As Range
Dim lAdd As String
Dim sName As String
Dim fileToOpen As String, a As String
Dim wdRng As Object, wdProtection As Long
On Error GoTo Msg
fileToOpen = Application _
.GetOpenFilename("WordDocumenten (*.doc*), *.doc*")
If fileToOpen <> "" And fileToOpen <> False Then
Msg: Err.Number = 0
Else: MsgBox "No file has been opened": Err.Number = 0: Exit Sub
End If
On Error GoTo ErrHandler
Err.Number = 0
Application.ScreenUpdating = False
Set rRng = Sheets(1).Range(Cells(2, 1), Cells(WordBereik, 1))
lScore = 100 'set the max possible score
'open the word documents
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.documents.Open(fileToOpen) 'ActiveWorkbook.Path & "\Formsword.doc")
'loop through the range
For Each rCell In rRng.Cells
Set rngName = rCell
'if the bookmark is filled in
If rCell.Offset(0, 1).Value <> "" Then
'store the data as variables
lAdd = rCell.Offset(0, 1).Value
sName = rCell.Value
If rCell.Offset(0, 2).Value = "Datum" Then lAdd = Format(lAdd, "d mmmm yyyy")
If rCell.Offset(0, 2).Value = "Bedrag" Then lAdd = Format(lAdd, "#,##0.00")
If wdDoc.Bookmarks.exists(sName) = True Then
For Each wdFormField In wdDoc.formfields
If UCase(sName) = UCase(wdFormField.Name) Then
a = "yes"
Exit For
End If
Next wdFormField
If a = "yes" Then
wdDoc.formfields(sName).TextInput.EditType Type:=wdRegularText, Default:=lAdd
a = ""
Else
UpdateBookmark sName, lAdd, wdDoc
End If
End If
End If
End If
Next rCell
wdDoc.fields.Update
Err.Clear
On Error Resume Next
Err.Number = 0
On Error GoTo ErrHandler
'With Selection.FormFields("Datum").Name = "Datum"
'With .TextInput.EditType Type:=wdRegularText, Default:=Format(Now, "d mmmm yyyy"), _Format:=""
'show the word document
wdApp.Visible = True
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
wdApp.Quit
Application.ScreenUpdating = True
MsgBox "Er heeft zich een fout voorgedaan"
End Sub
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String, WdDocument As Object)
Dim BMRange As Object, MyStart As Range
Set BMRange = WdDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
WdDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
Again: I am much obliged for all your advice and input, many thanks
!
Quote:
Originally Posted by macropod
That suggests you really don't understand what you're doing. Your code uses:
Set objDoc = .Documents.Add 'If no template
which indicates you're using Word's Normal template instead of a specific template for this project. Were you to create a template for this project, and use:
Set objDoc = .Documents.Add(Template:="Filepath\Filename.dotx")
where 'Filepath\Filename.dotx' represents the template's path and name, your code could be much simpler.
I also don't see any indication you've actually tried the code I posted or that you've paid any attention to my request in your other thread that you use code tags when posting code. I also reiterate my previous advice that you don't need to seek the header or select anything.
|