PDA

View Full Version : Solved: Updating Textfield in TextBox



fredrique
05-06-2005, 01:50 AM
Hi,
i would like to have a textfield
({includetext "Doc1.doc" MyName \*charformat}, which means that i include a text(bookmarked text called MyName) into a my document called from Doc1.doc)
i need text to be located in a specific location on the document(Verticaly in the left handside) and this text(field) need to be repeted in all other pages of the document.
what i did was, i added a text box and i placed the texfield in it. this allow the text to be repeted in all other pages. and when i run a macro for updating the field in the textbox, the update is performed succesfully.

This is the code that i use for updating:
'ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
'Selection.HeaderFooter.Shapes("Text Box 3").Select
'Selection.Fields.Update

RQ: this code i valid just when the document is not Protected
RQ: is use Word 2002
The Problem:
When i protect the documents (with/without passoword). the update can not be performed. this is basically because there is not ActiveWindow when document is protected.

i would like to update my textfield normally, even if the document is locked.
any help On This :mkay
thanks alot
Fredrique

MOS MASTER
05-06-2005, 02:43 AM
Hi Fredrique, :D

Welcome to VBAX! :hi:

Your looking for TextBox 3 in your Documents header. It could be that your document only has one Texbox in the header and it's name is "Text Box 3" in that case you can use something like:

Sub UpdateOne()
Dim oRange As Word.Range
Set oRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
oRange.ShapeRange("Text box 3").TextFrame.TextRange.Fields.Update
Set oRange = Nothing
End Sub


But Names of Shapes can differ quite a bit in documents and are usually hard to find. So you can avoid errors by addressing all shapes in the Headers shaperange!

Like:
Sub UpdateAllTextboxes()
Dim oRange As Word.Range
Dim oShape As Word.Shape
Set oRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
If oRange.ShapeRange.Count > 0 Then
For Each oShape In oRange.ShapeRange
oShape.TextFrame.TextRange.Fields.Update
Next
End If
Set oRange = Nothing
End Sub


Both of these sub's don't require the document protection to be off while executing the code.

You're code uses the Selection Object and if you use that one it is required to unprotect the document first. (Range objects don't)

I'm Not sure if the textbox is in your Header (That's my assumption in the code) or the Footer!

If it is in the Footer you can change every Headers by Footers in the Set Instruction for oRange!

Also a document could have 3 different kind of Headers my assumption is you are using the default 1 = wdHeaderFooterPrimary

If the sub trows an error or doesn't update properly it's posible you are using another kind off header. So experiment with: wdHeaderFooterEvenPages Or wdHeaderFooterFirstPage

Enjoy! :thumb

fredrique
05-06-2005, 03:57 AM
Thanks MOS Master

that was great!!:friends:

Fredrique

MOS MASTER
05-06-2005, 04:00 AM
Thanks MOS Master

that was great!!:friends:

Fredrique
Hi Fredrique, :D
You're Welcome! :beerchug: