index > Visual Studio Tools for Office > populate docvariables

populate docvariables


My VSTO project consists of the following:
created a MSWORD TEMPLATE with docvariables
ex: this is my {docvariable fname \*mergeformat}
this is my {docvariable lname \*mergeformat}
in the THISDOCUMENT_startup event I am successfully connecting to the database that includes the fields to fill the docvariables fname & lname.
ex: fname = rs("first"),
lname = rs("last")
my problem is when I run this in debug, my document appears, but the variable information is not updated with the data from the file that I have connected to. I step through my code and see that I have the fname & lname, the docvariables are not updated.

I have done something similar to this with VBA code and all works fine, but getting started with VSTO, I'm not having much luck with the docvariables. I'm looking for any suggestions or possible articles that will help me with VSTO, connecting to a database and populating variable fields.

Thanks,
Diane06

diane06

Hi Diane

Could you please show us the exact code lines you're working with, and a corresponding bit of VBA code so that we can compare/test?

What you show us can't work, as it stands. fname may well be a variable in your code, but it's not a Word Document Variable. That's an object of the Word object model. You'd need to do something along these lines

Dim fname as Word.Variable = doc.Variables("fname")
fname.Value = rs("first")




-- Cindy Meister (Word MVP)
Cindy Meister
Cindy,
Thank you for the example. I am putting together a simple fname & lname code that I will forward later, but first, could you explain my confusion on this:
I have a MSWORD template that includes my document variables fname & lname. When I created my new project, I took the choice to "copy an existing document" to my project. Since "fname" & "lname" is defined as docvariables in the MSWORD template(secdoc.dot) and this template is within my project, I was not defining "fname" & "lname" in my code as you indicated: "Dim fname as Word.Variable = doc.Variables("fname")
diane06

Hi Diane

The name of this collection and object in the Word object model is confusing, no question :-) Perhaps the Word developer team would have chosen something else, some twenty years ago, if they'd realized how programming languages were going to evolve...

No, the VSTO tool doesn't automatically pick up document VARIABLE objects (although it's definitely a fascinating thought). Think of a docVar (I'll use that term to try to help distinguish these from variables you use in your code) as a container in a document file, similar to a bookmark or a document property (as in File/Properties). It's just one more thing in the document binary file; one more thing Word provides.

So, in order to use it in any code (whether VSTO, VBA or something else), you first have to address the object (the docVar) for it to be recognized. Then you can access its content (the Value property). To get the content of a bookmark (just to continue the example), you do something like this

Dim bkm as Word.Bookmark
Dim fName as String
fName = rs("fname").Value
Set bkm = doc.Bookmarks("bookmarkName")
bkm.Range.Text = fName

A document property you access along these lines

Dim prop as Office.DocumentProperty
Dim fName as String
fName = rs("fname").Value
Set prop = doc.BuiltinDocumentProperties("Title")
prop.Value = fName

Correspondingly, for a docVar

Dim docVar as Word.Variable
Dim fName as String
fName = rs("fname").Value
Set docVar = doc.Variables("docVarName")
docVar.Value = fName

Hope this helps dissipate some of the confusion :-)




-- Cindy Meister (Word MVP)
Cindy Meister
Cindy,
Here is my very simple program , I'm having an issue with "doc.Variable("flname") see below:
I'm not sure if I'm missing an "Import" statement to have the "doc" declared??
************
Imports IBM.Data.DB2.iSeries
Public Class ThisDocument
Dim cn As New iDB2Connection
Dim cmd As New iDB2Command
Dim rs As iDB2DataReader


Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
If cn.State = ConnectionState.Closed Then
cn.ConnectionString = "DataSource=QMMSRVR; defaultCollection=mylib;"
cn.Open()

cmd.CommandText() = "Select * from mylib.myfile"
cmd.Connection = cn
rs = cmd.ExecuteReader
Call mergedata()
End If
End Sub

Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
End Sub


Public Sub mergedata()

Dim docvar As Word.Variable
Dim flname As String

Do While (rs.Read() = True)
flname = Trim(rs("nam"))
docvar = doc.variable("flname")
|--->I'm having a problem here,
program error on"doc
diane06
Cindy,
I have it!! THANK YOU......for your examples & suggestions!!
Once I changed the line ---> docvar = doc.variable("flname")
diane06

Hi Diane

I'm sorry. "doc" was just an arbitrary choice of term, designating any object variable of type Word.document. In a VSTO project you'd most often use ThisDocument.InnerObject.Variables(index).




-- Cindy Meister (Word MVP)
Cindy Meister
reply 7

You can use google to search for other answers

 

More Articles

Embedding VSTO Spreadsheet in .NET Windows Forms 2.0
Delete a range in VSO
.net COM Add-in for Excel - cannot find Extensibility Projects
Access Runtime
InfoPath - Explicit call to webservice in .NET code only works in...
Range.InsertFile and XPath
AddIns
Start Access DB Macro from VB.NET
VSTO training
VSTO August CTP COM Exception
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

Simple mail merge problem
VSTO Word doc design error when adding a…
Create manual and automated tests for VS…
How to print access report through print…
Make smart tag recognizer stop recognizi…
Office Pro Small Business Edition & …
Error In Action Pane Add In's
Custom font colors
AddIn-Debugging doesn't work anymore?!?!
CommandBarButton Selected?
How to uninstall Outlook Addin
Visual Studio 2005 for Office over VS St…
Need guidance on which VSTO tools to use…
Outlook Addin On Windows 2000 Terminal S…
How could i develop Office Add-ins compa…

Hot Articles

MenuButton click event not firing
Programming Excel - Data Analysis and Re…
How to get/calculate the character width…
Assembly not being loaded with MS-Word
Suspending the Word background repaginat…
Document Protection disables too much!?
BeforeItemPaste outlook explorer object …
Microsoft OutLook Plugin Problems
VSTO 2005 Outlook Add-In reply event not…
Outlook 2003 VSTO plugin doesn't run
Saving OLE Objects with Webservices Tool…
What is the difference between between v…
Does Outlook 2003 forget me?
deploying add-ins without installing PIA…
How to install VSTO on VS2005 Profession…

Recommend Articles

Dynamic Range in Excelsheet
Access Developer Extensions
Smart Tags and Action Pane in Word doc t…
Outlook 2003, VSTO 2005, and multiple e…
My Word COM add-in stops working - I do …
Deploying Outlook Add-In (after followin…
Question re the SetCurrentFormPage() met…
Using Access in VB.net
My VSTO could not work with SSL, Why?
VSTO SE2005 CustomTaskPane Samples...
Excel: show XML data horizontally
Word/Excel Designer Error in VSTO 2005
Deploying Shared AddIns created in C#.NE…
selecting no in a save changes msgbox fo…
Please tell me where "using Microso…