Solved

Passing data to word objects from vb apps

Posted on 2002-04-10
8
194 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
Comment Utility
' 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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:skhorshid
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Expert Comment

by:daffyduck14mil
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now