none
late-bonding cannot work RRS feed

  • Question

  • You cannot vote on your own post
    0

    hi

    Word could not fire the event where i have added late-bond into.

    code:

     closeButton = (CommandBarButton)oPreviewBar.FindControl(Type.Missing, 923, Type.Missing, Type.Missing, Type.Missing);
     closeButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(closeButton_Click);

    This code is to add the closeButton_Click into close button. and It run well on word 2003.

    I have no idea why this does not work for word 2007.

    Are there any changes between word 2003 and word 2007.

    Do you have experience with it?

    Wednesday, December 15, 2010 1:21 AM

All replies

  • Hi Livio

    You don't provide a lot of information...

    Have you placed your code in a try...catch block to ensure that all error messages are propagated back to your code? It's possible the code is failing, but not telling you why, if you don't have it in a try...catch block.

    It's not clear exactly which command this should be. I've queried some properties of control 923 in Office 2007 and it returns:

    Exits print preview
    &Close

    From my quick tests, it is available in Word 2007. It's only enabled when in the Print Preview view, and it does work to close print preview. So:

    You say it "doesn't work". Please describe HOW it does not work. I'm assuming that closeButton_click appears not to be executing. Please show us the code for that event.


    Cindy Meister, VSTO/Word MVP
    Wednesday, December 15, 2010 1:46 PM
    Moderator
  • hi Cindy Meister

    Thank you for your quick response!

    >It's not clear exactly which command this should be. I've queried some properties of control 923 in Office 2007 and it >returns:

    >Exits print preview
    >&Close

    As far as i know,923 is the id of control for the close print preview button.

    I get the infomation from the word.Application.CommandBars["Print Preview"].CommandBar.tooltipText.

    >From my quick tests, it is available in Word 2007. It's only enabled when in the Print Preview view, and it does work to >close print preview. So:

    But i failed. when i try to hook the event to my function.

    now i try to explain my work.

    1. I have an opened docx word.

    2. For the print previw, i have code to new word application.

    ( this.word = new Word.ApplicationClass();)

    3. and then open the docx word again in memory.

    ( this.doc = this.word.Documents.Open(ref objFileName,
                                                        ref oMissing, ref oReadOnly,
                                                        ref oMissing, ref oMissing,
                                                        ref oMissing, ref oMissing,
                                                        ref oMissing, ref oMissing,
                                                        ref oMissing, ref oMissing,
                                                        ref oMissing, ref oMissing,
                                                        ref oMissing, ref oMissing,
                                                        ref oMissing);)

    4. to excute the print preview with the code.

    ( this.word.ActiveDocument.PrintPreview();)

    5. this is success for the printpreview.

    when i run the code ,i can see the print preview of docx  with new.

    But there is an problem that when the user click the close print preview, new word can not be closed auto.

    So I have to add the event to the close print preview button for closing the new word.

    private void closeButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)

    (

                ((Word._Document)doc).Close(ref oSaveChanges, ref oMissing, ref oMissing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                word.Quit(ref oSaveChanges, ref oMissing, ref oMissing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
                word = null;

    )

    Please let me know if you have any additional questions

    Thank you!

    livio

     

     

     

    Thursday, December 16, 2010 1:39 AM
  • Hi Livio

    you still haven't said whether you've tested your code inside a try...catch block. You certainly must do this before we can continue the discussion.

    I also recommend you stick a MessageBox.Show() or some kind of logging into your code in order to determine what commands are (or are not) actually being executed. For example, on the line following the assignment of the Click event procedure and at that top of that procedure. That way you can determine exactly where things are "stopping".


    Cindy Meister, VSTO/Word MVP
    Thursday, December 16, 2010 7:14 AM
    Moderator
  • hi Cindy Meister

    I'm sorry for the pure explanation!

    I have tested whether the code is excuted.

    I inserted the debug point ,then run it.but don't stop at the point.

    By the same way,I tested the code with the word 2003,then it can be stopped at point.

    So,with the same soure,it is invalid in the word2003 ,not in word 2007.

    i think  there is anything wrong with the below source code in word 2007.

     closeButton = (CommandBarButton)oPreviewBar.FindControl(Type.Missing, 923, Type.Missing, Type.Missing, Type.Missing);
     closeButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(closeButton_Click);

    livio

     

    Thursday, December 16, 2010 7:58 AM
  • Hi Livio

    Again: how about the try...catch blocks? I really can't continue this discussion without you having put those in your code. They aren't "nice to have" or a "pain to have": they're a necessity when automating Office using .NET.

    <<I inserted the debug point ,then run it.but don't stop at the point.>>

    And where, exactly, was this break point?


    Cindy Meister, VSTO/Word MVP
    Thursday, December 16, 2010 8:21 AM
    Moderator
  • hi Cindy Meister

    now i have placed your code in a try...catch block as the below code.

     private void closeButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
            {
                try
                {
                    object oMissing = Type.Missing;
                    object oSaveChanges = false;
                    ((Word._Document)doc).Close(ref oSaveChanges, ref oMissing, ref oMissing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                    word.Quit(ref oSaveChanges, ref oMissing, ref oMissing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
                    word = null;

                    this.Activate();
                }
                catch
                {
                    MessageBox.Show("there is alert in the closeButton_Click");
                }
            }

    result:

    Run the print preview,then click the close print preview button.

    there was not any message while the opration.

    >><<I inserted the debug point ,then run it.but don't stop at the point.>>

    >>And where, exactly, was this break point?

    i added the break point at the first code on the closeButton_Click.

    Thursday, December 16, 2010 9:11 AM
  • <<now i have placed your code in a try...catch block as the below code.>>

    And also in the code that creates the delegate for the Click event, please. If that event is not being called, there may be a problem with creating the delegate (that's the line with +=)


    Cindy Meister, VSTO/Word MVP
    Thursday, December 16, 2010 3:30 PM
    Moderator
  • hi Cindy Meister

    I think so too.There may be a problem with creating the delegate .

    First,I can not confirm wether is impossible to create the delegate with the close print preview.

    As i know,in word 2007, i can create delegate by

    word.ApplicationEvents3_Event_DocumentBeforeClose or

    word.ApplicationEvents3_Event_DocumentBeforePrint and so .

    But i have not an success sample to create delegate with the button like colse print preview which hasn't been define in the events of Word.ApplicationClass.

    Do you have  success sample code like it?

    livio

    Friday, December 17, 2010 1:16 AM
  • hi Cindy Meister

    i found the same discussion at below page.

    http://social.msdn.microsoft.com/Forums/en/vsto/thread/f5075f05-c944-4260-af3c-f7fba3adf9c0

    you gave the adivce with the Ribbon xml.

    but i want to catch the event with the plain word file.

    Also i saw the way to check the Application.PrintPreview to walk around.

    but which event i add the check into?

    livio

    Friday, December 17, 2010 6:30 AM