Solved

Passing data to word objects from vb apps

Posted on 2002-04-10
8
195 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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

14 Experts available now in Live!

Get 1:1 Help Now