Sending an object to a form
I've written an add-in for MS Word which adds a commandbarbutton, and fires a form when clicked.
I'm having some trouble sending the Word application object to the form, so that it can work with my Word document.
Conceptual code looks like this:
file: connect.vb
Code:
public class
Dim withevents SuperForm as Windows.Forms.Form
Dim applicationObject as Object
applicationObject = application
private sub button_click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles Button.Click
SuperForm = New Mainform
SuperForm.Show()
end sub
end class
file mainform.vb
Code:
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim oWord As Word.Application
oWord = CType(applicationObject, Word.Application)
...
How can I send the current Word application object to the new form I'm creating?
Is is correct to use something like this?:
Code:
Public MainForm(ByRef applicationObject As Object)
and
SuperForm = new MainForm(applicationObject)
Re: Sending an object to a form
either overload the forms new constructor or place the object in the current Sub New for the form then make a private variable for that forms scope. hope this make sense
Re: Sending an object to a form
Hmm, I'm not quite fluid in speaking vb.net yet... Could you show a code-example?
Re: Sending an object to a form
ok first explain to me are you calling the same form again for the superform? or is the superform suppose to be in a seperate class? if not that's fine but personally i would make a seperate class that would inherit the mainform and just pass the word object...but agian i'm not 100% sure how you're coding this :ehh:
Re: Sending an object to a form
Ok, here is the flow of the program.
It's written as an Add-in for Word, and consists only of one form. The idea is that my form is created and shown when the user clicks the commandbarbutton in Word.
The issue I'm struggeling with is that my form needs to be able to work with the Word application it was called from.
The superform is in a different class and a different .vb file.
I actually wrote the program as a normal program with a single form. It was afterwards that I wrapped the program with the Word Add-in extensibility.
Re: Sending an object to a form
ok... so i'm assuming that in the event where you click the but that's
VB Code:
'This would be the code in the other class that is for MainForm.
Public Class MainForm : Inherits System.Windows.Forms
Private oWord As Word.Application
'This is in in the Form designer region
Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
Sub New(ByVal oWordApp As Word.Application)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
oWord = oWordApp
End Sub
End Class
'I would assume that this is within the word app
private sub button_click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles Button.Click
'I have never done an add-on but i would imagine that you have reference to
'the word app here so just pass reference for this app to the MainForm.
SuperForm = New Mainform(Me)
SuperForm.Show()
end sub
i hope this helps
Re: Sending an object to a form
That was exactly what I was looking for, it solved my problem superbly! Thanks a million, vbdotnetboy!
Re: Sending an object to a form
anytime...don't forget to mark it as resolved. :wave:
Re: Sending an object to a form
I'm having another, probably stupid, question.
I've written an eventhandler which handles SuperForm.Closed
Is this the correct way of disposing of my program/form?:
VB Code:
Private Dub FormClosed(ByVal sender as object, ByVal args as Eventargs) Handles SuperForm.Closed
SuperForm.dispose()
SuperForm = Nothing
End Sub
Re: Sending an object to a form
just delete the superform = nothing...all you need is the dispose...however in that click event you may want to set superform = nothing.