Results 1 to 10 of 10

Thread: Sending an object to a form

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    54

    Resolved 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)
    Last edited by Frode; Jan 10th, 2005 at 05:27 PM.

  2. #2
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    54

    Re: Sending an object to a form

    Hmm, I'm not quite fluid in speaking vb.net yet... Could you show a code-example?

  4. #4
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    54

    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.

  6. #6
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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:
    1. 'This would be the code in the other class that is for MainForm.
    2.  
    3. Public Class MainForm : Inherits System.Windows.Forms
    4.        Private oWord As Word.Application
    5.  
    6.        'This is in in the Form designer region
    7.  
    8.        Sub New()
    9.              MyBase.New()
    10.  
    11.              'This call is required by the Windows Form Designer.
    12.              InitializeComponent()
    13.              'Add any initialization after the InitializeComponent() call
    14.  
    15.        End Sub
    16.  
    17.        Sub New(ByVal oWordApp As Word.Application)
    18.              MyBase.New()
    19.  
    20.              'This call is required by the Windows Form Designer.
    21.              InitializeComponent()
    22.              'Add any initialization after the InitializeComponent() call
    23.              oWord = oWordApp
    24.        End Sub
    25.  
    26. End Class
    27.  
    28. 'I would assume that this is within the word app
    29.        private sub button_click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles Button.Click
    30.             'I have never done an add-on but i would imagine that you have reference to
    31.             'the word app here so just pass reference for this app to the MainForm.
    32.             SuperForm = New Mainform(Me)
    33.             SuperForm.Show()
    34.         end sub

    i hope this helps

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    54

    Re: Sending an object to a form

    That was exactly what I was looking for, it solved my problem superbly! Thanks a million, vbdotnetboy!
    Last edited by Frode; Jan 10th, 2005 at 05:26 PM.

  8. #8
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Sending an object to a form

    anytime...don't forget to mark it as resolved.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    54

    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:
    1. Private Dub FormClosed(ByVal sender as object, ByVal args as Eventargs) Handles SuperForm.Closed
    2. SuperForm.dispose()
    3. SuperForm = Nothing
    4. End Sub

  10. #10
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width