Passing data to word objects from vb apps

I have a vb app which creates a word application from a specific template.
In this template I have a macro in a the template which connects to a database for various data.
I need to be able to pass a string variable from my vb app to the word app so I can use the variable to connect to the db specified from my vb app.
Is it possible to create custom properties of a document that I can set from my vb code or some other way of passing the data.
I cant use an ascii file because I may have multiple instances of my vb application open with different data needing to be passed to the relevant child word document


Who is Participating?
Hi Ben,

Yes, it is possible to extent the document with custom properties. You do the following way:

- Open your template project
- Enter the Visual basic editor with Alt+F11
- Open the code window for the object "ThisDocument"
- Create your property like you do in Visual Basic (Private variable, and the property Get and Let). You need to do this manual, so in your case, i'll give an example below.)
- Save your template.
- In your normal macro module, you can now use this property, with for example, the syntax: ActiveDocument.ConnectionString.
- In Visual Basic itself, once you have set the document object to your template, you can also use oDocument.ConnectionString = "Something".

Now, as for the example property code. This you can pase in your template, in the object "ThisDocument":

Option Explicit

Private m_sConnectionString ' Local copy

Public Property Get ConnectionString() as string
  ConnectionString = m_sConnectionString
End Property

Public Property Let ConnectionString(Byval sNewValue as string)
  m_sConnectionString = sNewValue
end Property

And in your macro, you can use this:

Public Sub OpenConnection()
  '.. Instantiate e.d.
  g_cnConnection.ConnectionString = ActiveDocument.ConnectionString
  '.. Do you thing here
End Sub

I hope this helps.


' this is an example of how to use Word automation and run a macro called dosomthing  you can pass args if you need to

Dim ap As New Word.Application
Dim doc As New Word.Document
Set doc = ap.Documents.Open("C:\deleteme.doc")
ap.Visible = True
ap.Run "dosomthing", "arg1"

the macro/procedure  "dosomthing" is in thisdocument code window but could be any where
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

I hope this example is clear,

"dosomthing" represents your procedure in the word document that accepts the connection string from your VB app.

If you have several instances of your word document in the one application object you may need to prefix the macro name with the document name.  

eg "doc1!dosomthing", "arg" "doc2!dosomthing", "arg"


I have tried what you said, but that doesn't work with my version of MS-Word. I get an error from VB itself, saying wrong number of arguments or invalid property assignment. I don't know what version of MS-Word you work with, but fact is, when you use the property sollution, it doesn't matter how many documents are open, you use the object associated with that document to set your properties e.d. In fact, if you put your code that uses these properties in the document code window, you can call them through the document object as you do with your properties. This allows you to open multiple documents, and yet have different settings for them, acting different.


Of couse it works , i tested it.

Its not your question anyway !!!

You must have made a mistake !!

I agree  your solution is better but it requires more code.

If i were doing this project i would not include any code in the word document. I would do everything in the vb app or vb dll.  The main reason for this being versions control. If you have many different versions of your code flaoting arouonf in many word documents it will be difficult to manage version control


I didn't mean to offend you, I merely tried what you said, with given results, with the MS-Word version I use. I then asked what your version of MS-Word you are using. Perhaps my choice of words was not entirely correct.

Besides, it is not a question of "who is right", but what works best. That's what EE is about. Less is not always more, in terms of sollid sollutions, but that is also a personal choice.

Also, I agree on you with your comment on version control. With code in your template, it can get hard to maintain the application.

I'll retire from this question, let Ben say what he thinks.


BenBaronAuthor Commented:
Cheers that works a treat, although I understand what Skhorshid is saying about version control, but as I already have the problem with that it doesnt make it any harder.
Also Skhorshid Im not running the macro immeadiately from my vb app, it is activated by the user from a command bar so I dont it would work anyway, but thanx for the suggestion.

Also any ideas how I may create a vb exe that takes off the original macros and modules and replaces them with newer ones? This is so I can automate the funtion when I distribute a version update. I know how to access the VB code windows etc from vb but havent got the foggyest how to remove and re-add.
cheers for all the help

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.