Word 07 Interop

DPrometheus

Well-known member
Joined
Jan 7, 2009
Messages
48
Location
The Netherlands
Hello there,

The problem is the following:
How can I pass a Word.Document object from vba code to vb.net?

I have made a small program which wraps Word 2007 into a usercontrol (custom panel) but now I want to interact with the document I ran into some problems..
For some other customizations (extra buttons on the new Ribbon) I cannot call the function with correct parameters it seems.

I cannot pass the Word.Document object from vba to vb.net for some reason I dont know.

Can someone shine a light for me here?

vb.net
Code:
Public Class EcfToolForm
    Private _document As Word.Document

    Public Sub New()
         This call is required by the Windows Form Designer.
        InitializeComponent()
         Add any initialization after the InitializeComponent() call.
    End Sub

 This one doesnt work since vba cant use parameterized constructors
    Public Sub New(ByRef doc As Word.Document)
        InitializeComponent()
        _document = doc
    End Sub

 Extra function to replace above constructor. This now must be called after the constructor has been called.
    Public Sub LoadDoc(ByRef doc As Word.Document)
        _document = doc
    End Sub

 ...

VBA
Code:
Sub SpecialistButton_Click(control As IRibbonControl)
    On Error Resume Next
    
    Set toolbar = New Specialist07.EcfToolForm
    toolbar.LoadDoc (Word.ActiveDocument)
    toolbar.Show
    
     Err.Clear
End Sub

The library seems linked correctly (The form is shown (a toolbar, as you might have guessed ;) ) when the button is pressed, but as soon as the toolbar buttons are clicked it says object reference not set on the Word.Document object).
Can someone explain me what Im doing wrong?

Kind regards,
~DP
 
Im not a Word programmer, but your code looks ok to me.

I think youll have to trace this with debugging tools. Trace the actual assignment of _document = doc to make sure this is getting set right. (It must, but, well, trace it anyway...)

Then put have a breakpoint within the method that gets called when you click a toolbar button. Then trace/step into that code as well to see what is going on.

- Mike
 
thanks for helping me out, however.. this is my conclusion of debugging this project:
on the VBA side on the line of
Code:
toolbar.LoadDoc (Word.ActiveDocument)
Word.ActiveDocument equals the actual document I cant see the full object but he recognizes it (it has the right name property, and at least it aint Nothing
(since VBE doesnt have such neat debugging tools as VS08)

But on the other side things get a bit harder. Since I have to sign it each time and put the dll in the GAC I cant actually use breakpoints here (or dont know how, just a normal breakpoint wont do at least).
But after some logging I can determine that the document is NOT passed through and doc in the LoadDoc function resides empty aka Nothing

Code:
Public Sub LoadDoc(ByRef doc As Word.Document)
     _document = doc
End Sub

I already tried changing byref (maybe vba only supports byval) but without succes.
Someone ideas?
~DP
 
True, you dont need ByRef here, since you arent changing the instance, so, yes, ByVal makes more sense. But this isnt your problem (as you said).

I have no clue, Im afraid... Are you saying that the doc As Word.Document parameter is coming through as Nothing even though the argument passed in is non-null?? Can you show your code, including your logging calls for your LoadDoc method?
 
Sorry for the late response, but I havent been at work for a few days due to sickness.

Eventually I changed the form to contain several buttons on the outside of the panel which now interacts to the word document. This seems to solve this issue and since we already needed multiple outside buttons we can hold these as well..
It would have been prettier if they nicely integrated into eachother but hey, it works now :)
And it aint that bad after all ;)

Thanks for your time and suggestions!

~DP
 
Hi DPrometheus

I know youve told Mike youve found a way to work around the problem, however, I thought youd be interested in knowing how to pass a Word document object (or, indeed, any office object) from VBA to your .NET code.

You have to declare the parameter in the .NET code as type object. Then you explicitly cast it (in VB.NET use CType()). For example:

public void passWordDoc(object doc)
{

Microsoft.Office.Interop.Word.Document WordDoc = doc as Microsoft.Office.Interop.Word.Document;
WordDoc.ActiveWindow.Caption = "Tested!";
}

Since this is VB.NET, if you use Option Strict Off, you could also just continue with it being an object and use late-binding. Of course, then you wouldnt have any Intellisense.
 
Hi Cindy,

thanks for your response, since were close to the deadline right now I will keep it in mind for a v2. ;)

Though through curiosity I made a small test and it really works like a charm. :)

Thank you

~DP
 
Back
Top