How to pass a parameter to VBA from VB.NET?

N

Newspear

I want to pass a document name to Word 2003 from VB.NET

VB.NET Code is following:

Dim oWord As Word.ApplicationClass
Dim docName As String

docName = "My document"

'Start Word and open the document.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Add("My template.dot")

'Run the macros.
oWord.Run("SetDocumentName", docName)

'Quit Word.
oWord.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord)
oWord = Nothing

VBA code is following:

Dim DocumentName As String
Public Sub SetDocumentName(ByVal aName As String)
DocumentName = aName
MsgBox DocumentName
End Sub

Public Sub GetDocumentName()
Application.Selection.TypeText (DocumentName)
End Sub

When running, the messagebox show the DocumentName has been set to "My
document", but when call GetDocumentName, find the DocumentName is null
string.

When debugging, I find if I modify SetDocumentName as below, it is OK
Public Sub SetDocumentName() ' No parameter
DocumentName = "My document"
MsgBox DocumentName
End Sub

but if I modify it as following, it work wrong
Public Sub SetDocumentName(ByVal aName As String) ' A dummy
parameter
DocumentName = "My document"
MsgBox DocumentName
End Sub

Please tell me how to pass the parameter to VBA corectly. Thanks.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TmV3c3BlYXI=?=,

Office Application.Run method is "finicky". Although it's been changed in
Word since Office 97 to accept parameters, it will only do so under certain
circumstances.

My suggestion would be to write the information into a document Variable,
then read from that when your code needs the information
Dim oDoc as Word.Document = oWord.Documents.Add("My template.dot")
oDoc.Variables("DocName").Value = docName

'In the called procedure
Application.Selection.TypeText (oDoc.Variables("DocName").Value)
I want to pass a document name to Word 2003 from VB.NET

VB.NET Code is following:

Dim oWord As Word.ApplicationClass
Dim docName As String

docName = "My document"

'Start Word and open the document.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Add("My template.dot")

'Run the macros.
oWord.Run("SetDocumentName", docName)

'Quit Word.
oWord.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord)
oWord = Nothing

VBA code is following:

Dim DocumentName As String
Public Sub SetDocumentName(ByVal aName As String)
DocumentName = aName
MsgBox DocumentName
End Sub

Public Sub GetDocumentName()
Application.Selection.TypeText (DocumentName)
End Sub

When running, the messagebox show the DocumentName has been set to "My
document", but when call GetDocumentName, find the DocumentName is null
string.

When debugging, I find if I modify SetDocumentName as below, it is OK
Public Sub SetDocumentName() ' No parameter
DocumentName = "My document"
MsgBox DocumentName
End Sub

but if I modify it as following, it work wrong
Public Sub SetDocumentName(ByVal aName As String) ' A dummy
parameter
DocumentName = "My document"
MsgBox DocumentName
End Sub

Please tell me how to pass the parameter to VBA corectly.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top