Passing data to word objects from vb apps

Posted on 2002-04-10
Medium Priority
Last Modified: 2010-05-02
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


Question by:BenBaron
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Expert Comment

ID: 6930782
' 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"


Expert Comment

ID: 6930785
the macro/procedure  "dosomthing" is in thisdocument code window but could be any where

Accepted Solution

daffyduck14mil earned 1200 total points
ID: 6930796
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.



Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 6930800
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.  


ap.run "doc1!dosomthing", "arg"
ap.run "doc2!dosomthing", "arg"


Expert Comment

ID: 6930809

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.



Expert Comment

ID: 6930832
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


Expert Comment

ID: 6930844

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.



Author Comment

ID: 6930851
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


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question