bookmarks with docvariables

D

Diane

Group,
I am trying to figure out how I can update a document variable that resides
within a bookmark. The code " bk1.Range.InsertFile((parapath),
Link:=False)", inserts a file indicated by "parapath. Now I need to update
the docvariable field "addltxt" that is within the inserted "parapath".
ALSO, I cannot use "word.unlink.fields", since that would update all my
docvariable fields and I only need to update one field named "addltxt".

Please note, I have several docvariables within the created document, all is
updating properly EXCEPT for the one that is included in the "parapath".

Any thoughts would be appreciated!!
Diane
 
D

Doug Robbins - Word MVP

Dim bmRange as Range
Set bmRange = bk1.Range
bmRange.Select
Selection.InsertFile parapath, , , False
ActiveDocument.Bookmarks.Add bk1, bmRange
bmRange.End = Selection.Range.End
bmRange.Fields.Update

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Diane

Doug,
I'm still having a problem with this, but something that would solve my
problems is if I could define the name of my "docvariable" within my code.
Hope you can follow me......Let's say I have 20 MSWORD documents, each
document has a docvariable name by the document name with "addltxt"
concantenated. So document ABC has a docvariable named ABCaddltxt, document
DEF has a docvariable named DEFaddltxt, etc.... NOW in my code, I read a
database file, from a field in the database, it tells me I want document ABC,
so now in my code, I want to do something like dim HOLD = docname &
"addltxt". The next step would be to define a word.docvariable based on the
information contained in the HOLD field. Is this possible??
 
N

Neil Cumfer

Your main problem is bigger than the bookmark problem.

When you insert a file into a Word document, the docvariable field is
included, but the document variable remains in the original file.

So the first thing you need to do, after inserting the file, is to add
the document variable to the destination document.

It is not clear whether, in your situation, the source file and the
destination document both already have a document variable with the same
name.

And if so, it is not entirely clear whether you actually want to update
the docvariable field or to update the document variable.

I have included some code which will add the document variable to the
destination document. Basically, what it does is locate all the
docvariable fields and makes sure that there is a document variable for
each field and that the contents of each document variable is the same
as that data that is shown in the docvariable field. This may not be
what you want because it assumes that the data showing in the field
codes is up-to-date and forces the document variable to match the field
code. But you can add an If statement to restrict it to a specified
variable name.

If you use this code (not guaranteed to be foolproof but it should work
if the docvariable field is in the correct format), you should be aware
that it moves the insertion point/selection and you will have to add
some code to account for that.

Dim myField As Field
Dim strName As String

For Each myField In ActiveDocument.Fields
If myField.Type = wdFieldDocVariable Then

myField.ShowCodes = True
Selection.HomeKey unit:=wdStory
Selection.Start = myField.Code.Start
Selection.MoveStartUntil cset:=Chr(34)
Selection.MoveStart
Selection.MoveEndUntil cset:=Chr(34)
strName = Selection.Text
ActiveDocument.Variables(strName).Value = myField.Result
myField.ShowCodes = False

End If
Next myField
 
D

Diane

Neil,
Your detailed post is very helpful. I will be testing with this today and
comment back. It is amazing the people on this forum that give their time to
explain in such detail to people like me who are trying to learn &
understand. It is only through forums such as this that I have learned about
the document variables, and still sometimes confusing myself. Again, your
post is very much appreciated!!

I'll be back for more.
Diane
 

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

Similar Threads


Top