![]() |
|
|
Thread Tools | Display Modes |
#5
|
|||
|
|||
![]()
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 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 Quote:
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Amapola188 | Word VBA | 3 | 07-12-2012 05:16 PM |
Excel Pivot Table Calculated Field | BertLady | Excel | 0 | 05-21-2012 10:51 AM |
Late Binding on SHDocVw | tinfanide | Excel Programming | 1 | 05-19-2012 10:00 PM |
![]() |
b0x4it | Word VBA | 4 | 05-26-2011 01:14 AM |
![]() |
b0x4it | Word VBA | 11 | 05-19-2011 11:15 PM |