Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 07-12-2012, 01:59 PM
Amapola188 Amapola188 is offline Windows XP Office 2010 32bit
Novice
 
Join Date: Mar 2011
Location: Auckland, New Zealand
Posts: 4
Amapola188 is on a distinguished road
Default Open header to insert text into bookmark

Good Morning, my code breaks at line 3 (excluding comments) of below code. It says it can't find the bookmark. I have checked that the bookmark is there and spelling etc is all correct. - I suspect that it may not open the header and therefore don't find the bookmark in the main document?



Anybody any idea why this would be.

Code:
      
        'Open Header
         Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
         ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        
        'Insert Data into Header
         Selection.GoTo What:=wdGoToBookmark, Name:="BMQuoteNumberHeader"
         Selection.TypeText Text:=txtQuoteNumber.Text
        
        'Close Header
         ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Thank you!

Christine
Auckland, NZ
Reply With Quote
  #2  
Old 07-12-2012, 02:50 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,048
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Hi Christine,

Try:
Call UpdateBookmark("BMQuoteNumberHeader", txtQuoteNumber.Text)
with:
Code:
Sub UpdateBookmark(BmkNm As String, NewTxt As String)
Dim BmkRng As Range
With ActiveDocument
  If .Bookmarks.Exists(BmkNm) Then
    Set BmkRng = .Bookmarks(BmkNm).Range
    BmkRng.Text = NewTxt
    .Bookmarks.Add BmkNm, BmkRng
  End If
End With
Set BmkRng = Nothing
End Sub
Note: There is no need for the GoTo, to explicitly access the header, or to select anything. Furthermore, unlike your code, if you change your mind about what should appear in the header, the content gets changed - your's just appends the new content.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 07-12-2012, 03:10 PM
Amapola188 Amapola188 is offline Windows XP Office 2010 32bit
Novice
 
Join Date: Mar 2011
Location: Auckland, New Zealand
Posts: 4
Amapola188 is on a distinguished road
Default Works perfectly

Thanks, Paul.
Code now running perfectly! - Have a good day, can't be bare sunrise yet!
Christine
Reply With Quote
  #4  
Old 07-12-2012, 05:16 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,048
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Nah, sun 'd been up 3/4hr already.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Repeating Bookmark Text BECKTHOMO Word 1 03-27-2012 08:34 PM
Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control skarden Word 1 12-12-2011 10:39 PM
Insert a header name in the text (body) bal-007 Word 3 11-25-2011 01:08 PM
delete all bookmark text hklein Word VBA 4 08-10-2011 04:33 AM
VBA to insert Bookmark rockwellsba Word VBA 2 05-31-2011 01:07 AM


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


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft