MS Office Forum / Word / Programming / October 2006
Activedocument.variables
|
|
Thread rating:  |
Karen - 15 Oct 2006 23:10 GMT Hi All,
I have a template. One of the features of the template was that certain fields in the header would be common to documents created from that template and yet different for individual users. For instance, the header has fields for camera, lens, ISO. A particular user may routinely use a Nikon, 8.9 to 89 mm lens and 400 ISO so the idea was that I would save those values as activedocument.variables and every time the user used the template, those values would be loaded into the template.
I distributed the template to a user with the values pre-filled in as camera, lens, ISO, etc. When the user tries to create a document from the template, he gets a runtime error saying that the variables are not available. I commented out the code and the template works fine. I only have one user who is trying this template so I don't know if the problem is isolated to him or not; I suspect it is a problem.
Am I mistaken that the distributed template should have the variables pre-loaded with the ordinary text? Any ideas on how I could troubleshoot this problem? It works fine on my systems............
Karen
Jay Freedman - 16 Oct 2006 01:57 GMT First, let me ask why you're using code at all. Why not just place DocVariable fields in the header of the template? They'll update as soon as a new document is created from the template (and yes, they will pick up the values of the variables in the template -- I tried it).
If you must use code, that works ok for me, too, with something simple like this:
Sub AutoNew() Dim rg As Range Set rg = ActiveDocument.Sections(1) _ .Headers(wdHeaderFooterPrimary).Range rg.Fields.Add Range:=rg, _ Type:=wdFieldDocVariable, Text:="test1" Set rg = Nothing End Sub
If you can't spot the problem, please post your code.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>Hi All, > [quoted text clipped - 18 lines] > >Karen Karen Hagerman - 16 Oct 2006 02:20 GMT Thanks Jay, let me check your suggestion; this is my code
Private Sub Document_New() 'open the header
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
'get current values for Camera, ISO, Flash, Lens and Photographer
ActiveDocument.Bookmarks.Item("DateTaken").Range.Text = _ Date ActiveDocument.Bookmarks.Item("IsoSpeed").Range.Text = _ ActiveDocument.Variables("templateISOSpeed").Value ActiveDocument.Bookmarks.Item("Flash").Range.Text = _ ActiveDocument.Variables("templateFlash").Value ActiveDocument.Bookmarks.Item("Camera").Range.Text = _ ActiveDocument.Variables("templateCamera").Value ActiveDocument.Bookmarks.Item("Lens").Range.Text = _ ActiveDocument.Variables("templateLens").Value ActiveDocument.Bookmarks.Item("Photographer").Range.Text = _ ActiveDocument.Variables("templatePhotog").Value
'close the header pane
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument ActiveWindow.View.Type = wdPrintView
'save the template
ActiveDocument.AttachedTemplate.Saved = TrueEnd Sub
End Sub
On the user's computer, this code stops the document with a message that the variables are not present.
Karen
Jay Freedman - 16 Oct 2006 15:56 GMT As far as the use of the document variables goes, I don't see anything that should cause a problem if the variables are indeed defined in the template.
On the user's computer, make a document based on the template. When the AutoNew macro stops on the error, open the Immediate window in the VBA editor (Ctrl+G), type in this line and hit Enter:
?ActiveDocument.Variables.Count
(the question mark at the beginning is shorthand for Debug.Print). Does it say zero or some other number?
Is that user on the same version of Word as you? What version(s) are we dealing with?
Completely unrelated to this problem, I'll point out that you should get rid of the code that moves the cursor into the header pane. If the bookmarks are defined in the header of the template, then all you need in the Document_New macro is this:
Sub Document_New() ActiveDocument.Bookmarks("DateTaken").Range.Text = _ Date ActiveDocument.Bookmarks("IsoSpeed").Range.Text = _ ActiveDocument.Variables("templateISOSpeed").Value ActiveDocument.Bookmarks("Flash").Range.Text = _ ActiveDocument.Variables("templateFlash").Value ' the rest of the bookmark/variable assignments End Sub
The header/main document folderol is unnecessary.
You also don't need the .AttachedTemplate.Saved statement unless something else is happening that could "dirty" the template itself (e.g., changing a variable value in the template -- which doesn't appear to be part of this code). And that statement doesn't actually save the template anyway; it just lies to Word and tells it not to check whether there are any changes that need to be saved.
 Signature Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
> Thanks Jay, let me check your suggestion; this is my code > [quoted text clipped - 44 lines] > > Karen Karen - 16 Oct 2006 17:05 GMT Hi Jay,
Thank you very much for the assist.
I have cleaned up the code now, so it looks like this:
Private Sub Document_New()
'set Photo toolbar and tooltips CommandBars("Photo").Visible = True CommandBars("Photo").Controls("Insert Photo Group").TooltipText = "Insert All Photos from Folder" CommandBars("Photo").Controls("Add Top Label").TooltipText = "Add Top Label" CommandBars("Photo").Controls("Add Caption").TooltipText = "Add Captions" CommandBars("Photo").Controls("Remove Top Label").TooltipText = "Remove Top Label" CommandBars("Photo").Controls("Edit Header").TooltipText = "Edit Header" CommandBars("Photo").Controls("Insert Single Photo").TooltipText = "Insert Individual Photos" CommandBars("Photo").Controls("Refresh TOC").TooltipText = "Update Table of Contents" CommandBars("Photo").Controls("DeletePhoto").TooltipText = "Delete Selected Photo"
ActiveDocument.Bookmarks.Item("DateTaken").Range.Text = _ Date ActiveDocument.Bookmarks.Item("IsoSpeed").Range.Text = _ ActiveDocument.Variables("templateISOSpeed").Value ActiveDocument.Bookmarks.Item("Flash").Range.Text = _ ActiveDocument.Variables("templateFlash").Value ActiveDocument.Bookmarks.Item("Camera").Range.Text = _ ActiveDocument.Variables("templateCamera").Value ActiveDocument.Bookmarks.Item("Lens").Range.Text = _ ActiveDocument.Variables("templateLens").Value ActiveDocument.Bookmarks.Item("Photographer").Range.Text = _ ActiveDocument.Variables("templatePhotog").Value
End Sub
The user has Word 2003 which is what I have so.............
I will DOUBLECHECK that the distributed template really has those variables and will work with the user to check if the count matches when it errors out.
Again, I really appreciate the help and will post here whether it is fixed
:) It will probably take a day or two to arrange a time :) Karen
As far as the use of the document variables goes, I don't see anything that should cause a problem if the variables are indeed defined in the template.
On the user's computer, make a document based on the template. When the AutoNew macro stops on the error, open the Immediate window in the VBA editor (Ctrl+G), type in this line and hit Enter:
?ActiveDocument.Variables.Count
(the question mark at the beginning is shorthand for Debug.Print). Does it say zero or some other number?
Is that user on the same version of Word as you? What version(s) are we dealing with?
Completely unrelated to this problem, I'll point out that you should get rid of the code that moves the cursor into the header pane. If the bookmarks are defined in the header of the template, then all you need in the Document_New macro is this:
Sub Document_New() ActiveDocument.Bookmarks("DateTaken").Range.Text = _ Date ActiveDocument.Bookmarks("IsoSpeed").Range.Text = _ ActiveDocument.Variables("templateISOSpeed").Value ActiveDocument.Bookmarks("Flash").Range.Text = _ ActiveDocument.Variables("templateFlash").Value ' the rest of the bookmark/variable assignments End Sub
The header/main document folderol is unnecessary.
You also don't need the .AttachedTemplate.Saved statement unless something else is happening that could "dirty" the template itself (e.g., changing a variable value in the template -- which doesn't appear to be part of this code). And that statement doesn't actually save the template anyway; it just lies to Word and tells it not to check whether there are any changes that need to be saved.
 Signature Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Karen Hagerman wrote:
> Thanks Jay, let me check your suggestion; this is my code > [quoted text clipped - 44 lines] > > Karen Karen Hagerman - 16 Oct 2006 02:22 GMT Oh,
And the variables have already been added, the point is to distribute this with the variables already defined and added. If a user changes the values then they will be saved and, from that point forward, the template will open with the user values.
Karen
First, let me ask why you're using code at all. Why not just place DocVariable fields in the header of the template? They'll update as soon as a new document is created from the template (and yes, they will pick up the values of the variables in the template -- I tried it).
If you must use code, that works ok for me, too, with something simple like this:
Sub AutoNew() Dim rg As Range Set rg = ActiveDocument.Sections(1) _ .Headers(wdHeaderFooterPrimary).Range rg.Fields.Add Range:=rg, _ Type:=wdFieldDocVariable, Text:="test1" Set rg = Nothing End Sub
If you can't spot the problem, please post your code.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
On Sun, 15 Oct 2006 16:10:25 -0600, "Karen" <wonderlover@functiy.com> wrote:
>Hi All, > >I have a template. One of the features of the template was that certain >fields in the header would be common to documents created from that template >and yet different for individual users. For instance, the header has fields >for camera, lens, ISO. A particular user may routinely use a Nikon, 8.9 to >89 mm lens and 400 ISO so the idea was that I would save those values as >activedocument.variables and every time the user used the template, those >values would be loaded into the template. > >I distributed the template to a user with the values pre-filled in as >camera, lens, ISO, etc. When the user tries to create a document from the >template, he gets a runtime error saying that the variables are not >available. I commented out the code and the template works fine. I only >have one user who is trying this template so I don't know if the problem is >isolated to him or not; I suspect it is a problem. > >Am I mistaken that the distributed template should have the variables >pre-loaded with the ordinary text? Any ideas on how I could troubleshoot >this problem? It works fine on my systems............ > >Karen >
|
|
|