docvariable saved in a template

D

Diane

I have created a vb program that creates an msword 2003 document based on a
template named sec1.
example:

1) oDoc = oWord.Documents.Add("C:\template\sec1.dot")


code is processed that inserts text from other documents:
2) oWord.Selection.TypeText(Text:="DOCVARIABLE myfilepath")

Some of the inserted documents include a docvariable “giftlineâ€. My code
processes the information that should update the text in the giftline
docvariable, although a strange thing has happened, I am now getting the same
repeated text in my giftline docvariable for all documents that DO NOT
REQUIRE THE GIFTLINE INFO.

After debugging my program, I have found that when I open the base template
“sec1†(from line #1 above) the strange text appears in my giftline
docvariable, I can visibly see it in my debugger. So, my question is, it
appears that the “docvariable giftline†resides SOMEWHERE in my template.
Without deleting this template and rebuilding it, is it possible to see this
docvariable, (similar like the macros, or the autotext) – if so, I don’t know
where.
 
D

Doug Robbins - Word MVP

With the template as the active document, press Alt+F9 to display the field
codes.

--
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, originally posted via msnews.microsoft.com
 
L

Lene Fredborg

There is no command in the user interface you can use to show all the
DocVariables but you can use a macro. The following macro will show the names
and values of all variables in the active document in a message box. In case
of many variables and/or values with many characters, you may need to change
the macro so that it shows one variable at a time due to the limitation of
characters accepted in a MsgBox:

Sub ShowAllVariablesInDoc()

Dim oVar As Variable
Dim Msg As String

Msg = "Variables in active document:" & vbCr & vbCr
For Each oVar In ActiveDocument.Variables
Msg = Msg & oVar.name & vbTab & vbTab & oVar.Value & vbCr
Next oVar

MsgBox Msg, vbOKOnly, "Variables in Active Document"

End Sub


If needed, you can delete a DocVariable as follows:

ActiveDocument.Variables("NameOfVariableHere").Delete

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
D

Diane

Lene & Doug,
Lene - your macro was perfect. It showed me everything that is invisible to
me.
Doug - the alt + F9, I use this many times, although in this situation, the
docvariable that is causing me grief IS NOT even displayed when I switch to
code view.

I now realize, if I add the docvariable "giftline", and switch back & forth
from code to text, it displays the text that keeps appearing in all my
documents.

This appears to be happening:

My base template SHOULD include only docvariables for: DATE, NAME, ADDRESS,
SALUTATION. When I execute the macro from Lene, this base template has 17
docvariables in it, NOT 4. The text from the other 13 are only visible when
I add each docvariable to the base template and then switch from code view to
text view.

When my program runs, it is somehow updating my base template (sec1.dot)
with the other docvariables. I'm not sure where or how, but will need to go
back to my project code and try to determine the problem.

If either of you have additional thoughts to this, I would be interested.
Both of you have been very helpful in determining this problem.

Many thanks!!
 
L

Lene Fredborg

In your code, you should check the parts where you add or manipulate
variables. The problem could be that you store the variables in the base
template instead of the document where they were expected to go (or – if the
variables are not missing in any of the documents – in both the template and
the document).

Since I have not seen your code, I cannot give you precise advice. But if,
for example, you use something like "ActiveDocument.Variabels…", it could be
that the active document is not the one you expect it to be. In macros that
manipulate different documents, it is a good idea to keep track of the
documents by assigning each document to a variable. For example:

Dim oDoc As Document
Dim oDoc_New As Document

Set oDoc = ActiveDocument

You can now refer to that specific document using oDoc and it does not
matter whether another document has become the active document in between:

With oDoc.Styles(wdStyleBodyText)
.Font.Size = 10
.ParagraphFormat.SpaceAfter = 9
End With

'If you add a new document
Set oDoc_New = Documents.Add

This way you can keep track of all the documents your macro needs to
manipulate and you can make changes to the individual documents without first
activating them. Just refer to the relevant variable.

Clean up when finished

Set oDoc = Nothing
Set oDoc_New = Nothing

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
D

Diane

Lene -
I'm following your explanation and below is just some snippets of my code,
but I think I should be OK. I'm assuming my template was updated several
weeks ago when I was testing some code, but didn't come to light until I was
putting my complete project to the test.

By using the "oDoc = OWord.Documents.Add("c:\template\sec1.dot"), do you
agree that my template\sec1 isn't open for an update. Rather, I've created a
new document based on the template - do you agree?

**********************************
'read database file,
'determine document inserts,
'create document based on template

oDoc = oWord.Documents.Add("C:\template\sec1.dot")

‘ process variable info here.
'update the fields
'remove the link

oDoc.Fields.Update()
oDoc.Fields.Unlink()
oWord.Selection.EndKey(Unit:=Word.WdUnits.wdStory)
 
L

Lene Fredborg

I agree that your code – with an adjustment – will create a new document
based on your template. Since a document is an object, you need to add the
Set statement in front of odoc:

Set oDoc = Documents.Add("C:\template\sec1.dot")

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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