Solved

Passing data to word objects from vb apps

Posted on 2002-04-10
8
196 Views
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

cheer

Ben
0
Comment
Question by:BenBaron
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:skhorshid
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"
ap.Quit

0
 
LVL 2

Expert Comment

by:skhorshid
ID: 6930785
the macro/procedure  "dosomthing" is in thisdocument code window but could be any where
0
 
LVL 3

Accepted Solution

by:
daffyduck14mil earned 300 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.

Grtz.©

D.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Expert Comment

by:skhorshid
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.  

eg

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

0
 
LVL 3

Expert Comment

by:daffyduck14mil
ID: 6930809
Skhorshid,

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.

Grtz.©

D.
0
 
LVL 2

Expert Comment

by:skhorshid
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


0
 
LVL 3

Expert Comment

by:daffyduck14mil
ID: 6930844
Skhorshid,

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.

Grtz.©

D.
0
 

Author Comment

by:BenBaron
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

Ben
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

778 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