Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 02-03-2013, 03:09 PM
Hdr Hdr is offline Adding field in word in header in a bookmark in table with Excel vba Late Binding Windows 7 64bit Adding field in word in header in a bookmark in table with Excel vba Late Binding Office 2007
Novice
Adding field in word in header in a bookmark in table with Excel vba Late Binding
 
Join Date: Feb 2013
Posts: 5
Hdr is on a distinguished road
Default Adding field in word in header in a bookmark in table with Excel vba Late Binding

L.S.,

I am trying to add fields in word with excel vba with late binding.
This works for simple bookmarks, but i need to add a field to a bookmark in a table/shape in the header.



I have the following code, in which you see that most works, accept the line that i need to have working. Could someone please help?

Thanks in advance!

Vance

Here is the code:
Code:
Sub CreateWordDocFromExcel_BM() 'LATE BINDING METHOD - Reference to Word not required. Dim objWord As Object 'Note Object in lieu of Word.Application Dim objDoc As Object 'Note Object in lieu of Word.Document Dim strTemplatePathAndName As String 'Late binding requires constants to be declared with values Const wdPaneNone = 0 Const wdOutlineView = 2 Const wdPrintView = 3 Const wdNormalView = 1 Const wdSeekCurrentPageHeader = 9 Const wdSeekCurrentPageFooter = 10 Const wdFieldEmpty = -1 Const wdSeekMainDocument = 0 Const wdAlignParagraphCenter = 1 Const wdAlignParagraphRight = 2 'Try GetObject first in case Word Application is already open. Set objWord = Nothing On Error Resume Next Set objWord = GetObject(, "Word.Application") On Error Goto 0 If objWord Is Nothing Then 'Word not open so create object Set objWord = CreateObject("Word.Application") End If With objWord 'Create a new Word document .Visible = True 'Can be false Set objDoc = .Documents.Add 'If no template AppActivate (objDoc.Name) End With With objWord If .ActiveWindow.View.SplitSpecial <> wdPaneNone Then .ActiveWindow.Panes(2).Close End If If .ActiveWindow.ActivePane.View.Type = wdNormalView Or .ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then .ActiveWindow.ActivePane.View.Type = wdPrintView End If 'Insert header (bookmark MySpot is in Header, Bookmark Myspots is in table in header, Bookmark MyReference is in normal text) .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader 'Following text works: .Selection.Fields.Add Range:=.Selection.Range, Type:=wdFieldEmpty, Text:= _ "REF MyReference \*Charformat \*Mergeformat ", PreserveFormatting:=True 'Following text works: .bookmarks("MySpot").Select 'Following text works: objDoc.bookmarks("MySpot").Select Set MyBookmark = objDoc.bookmarks("MySpot").Range MyBookmark.Text = "" 'Following text works, in table a whole field is bookmarked as "Myspots" - so the bookmark does not dissappear Set MyBookmark = objDoc.bookmarks("MySpots").Range MyBookmark.Text = "" 'Next does NOT work and I need this to work (adding a field in a header in a table or shape) MyBookmark.Fields.Add Range:=MyBookmark, Type:=wdFieldEmpty, Text:= _ "REF MyReference \*Charformat \*Mergeformat ", PreserveFormatting:=True End With 'Clean up Set objDoc = Nothing Set objWord = Nothing End Sub

Last edited by macropod; 02-03-2013 at 11:50 PM. Reason: Added code tags & formatting
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding field in word in header in a bookmark in table with Excel vba Late Binding Open header to insert text into bookmark 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
Adding field in word in header in a bookmark in table with Excel vba Late Binding Putting a bookmark on a REF field in the selection b0x4it Word VBA 4 05-26-2011 01:14 AM
Adding field in word in header in a bookmark in table with Excel vba Late Binding Highlight and put bookmark on the closest field to the cursor b0x4it Word VBA 11 05-19-2011 11:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft