VBA and bookmark in a textbox in header/footer

C

Camilla

I have a template for 20 different localoffice. The user
choose in a form which office they belong and then click
OK and it will be correct inserted in the pagehader. I
have all officeaddress as autotext, the code choose
correct address and put it in the textbox with this code:
ActiveDocument.Bookmarks("dn_Adress").Select
SaveSetting "DN", "Configuration", "DefaultAdress",
Me.CombAdresser.Text
Select Case Me.CombAdresser.Text
Case "Falun" '/Falun
ActiveDocument.AttachedTemplate.AutoTextEntries
("dn_Adr_Falun").Insert Selection.Range, True
Case "Gävle" '/Gävle
ActiveDocument.AttachedTemplate.AutoTextEntries
("dn_Adr_Gävle").Insert Selection.Range, True.

The bookmark is placed in pageheader in a textbox.
My very big problem is that I must reload the userform to
let the user change the address and when I do that will
the new officeaddress appear after the first one, I need
help how I can select and replace the text with the new
address. Can anyone help me with this?

Thanks for any help
Camilla
 
M

Martin Sägesser

Hi Camilla

it is easier to use the Range Object, you don't have to open the header
nor select the textbox...
Because the bookmark is redefind at the end the existing text gets
replaced.

Dim rng As Range
Set rng = ActiveDocument.Bookmarks("dn_Adress").Range
With rng
.Delete
.InsertAfter "dn_Adr_" & Me.CombAdresser.Text
.InsertAutoText
End With
ActiveDocument.Bookmarks.Add "dn_Adress", rng


hth, Martin
 
C

Camilla

Sorry, but I don't understand exactly how I can use your
example in this code. Every Autotextentries has their own
adress, phone etc and teh Select case make choice which
one will be inserted.

ActiveDocument.Bookmarks("dn_Adress").Select
SaveSetting "DN", "Configuration", "DefaultAdress",
Me.CombAdresser.Text

Select Case Me.CombAdresser.Text

Case "Falun" '/Falun
ActiveDocument.AttachedTemplate.AutoTextEntries
("dn_Adr_Falun").Insert Selection.Range, True

Case "Gävle" '/Gävle
ActiveDocument.AttachedTemplate.AutoTextEntries
("dn_Adr_Gävle").Insert Selection.Range, True.

Regards Camilla
 
D

Dave Lett

Hi Camilla,

Using the Range object, as Martin suggested, only works if you have
enclosing bookmarks. However, it's clear from your description that you have
a placeholder bookmark. Take a look at the article "Working with Bookmarks
in VBA" at http://www.mvps.org/word/FAQs/MacrosVBA/WorkWithBookmarks.htm.
After that have a look at the article "Inserting text at a bookmark without
deleting the bookmark" at
http://www.mvps.org/word/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

After you create an enclosing bookmark in your template, you can use the
methods described in the second article.

HTH

Sorry, but I don't understand exactly how I can use your
example in this code. Every Autotextentries has their own
adress, phone etc and teh Select case make choice which
one will be inserted.

ActiveDocument.Bookmarks("dn_Adress").Select
SaveSetting "DN", "Configuration", "DefaultAdress",
Me.CombAdresser.Text

Select Case Me.CombAdresser.Text

Case "Falun" '/Falun
ActiveDocument.AttachedTemplate.AutoTextEntries
("dn_Adr_Falun").Insert Selection.Range, True

Case "Gävle" '/Gävle
ActiveDocument.AttachedTemplate.AutoTextEntries
("dn_Adr_Gävle").Insert Selection.Range, True.

Regards Camilla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top