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.
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.