none
Problem with Add-In For Word -- Add-In doesn't recognize open windows in Word RRS feed

  • Question

  • Hello!

    I have a lot of experience writing VBA macros in Word, but I am a beginner to Visual Studio.  I have created a simple ribbon Add-In with Ribbon Designer that has two buttons:  (1) Cleanup Button; and (2) Format Button.  Cleanup Button, when clicked, executes a macro called "ShowTableCleanup", and Format Button, when clicked, executes a macro called "ShowFinTable".  Both macros reside in my normal template in Word.  Each of these macros checks to make sure that a document (window) is open in Word otherwise the remaining code doesn't run.  Following is the vba code at the start of each macro:

    If Application.Windows.Count = 0 Then

        Exit Sub

    End If 

    Following is the code in ribbon.vb in my Visual Studio project:

    Imports Microsoft.Office.Tools.Ribbon

     

    Public Class November25_17AddInRibbon

        Public oWord As Word.Application

        Private Sub November25_17AddInRibbon_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load

            oWord = New Word.Application

        End Sub

     

        Private Sub CleanupButton_Click(sender As Object, e As RibbonControlEventArgs) Handles CleanupButton.Click

            oWord.Run("ShowTableCleanup")

        End Sub

     

        Private Sub FormatButton_Click(sender As Object, e As RibbonControlEventArgs) Handles FormatButton.Click

            'MsgBox(oWord.Windows.Count)

            oWord.Run("ShowFinTable")

        End Sub

    End Class

    I did not do anything to the code in ThisAddIn.vb.  Following is the code in ThisAddIn.vb:

    Public Class ThisAddIn

        Private Sub ThisAddIn_Startup() Handles Me.Startup

        End Sub

        Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

        End Sub
    End Class

    Even when more than one document is open in Word, the code in ribbon.vb returns the value of open windows as "0" (zero).

    To test it, I've put the following line of code in Private Sub FormatButton_Click:

    MsgBox (oWord.Windows.Count)

    MsgBox always returns "0"

    When executed from within Word, each macro runs flawlessly.  But when executed by pressing a button in the add-in, I end up getting a run-time error similar to:  "no window is open, so the code could not be executed".

    If someone could help me out, I would greatly appreciate it.

    Thank you.

    Paul

    • Moved by 宝宝徐 Tuesday, November 28, 2017 5:39 AM
    Monday, November 27, 2017 4:29 PM

Answers

  • Hi SimplyPaul,

    you can try to fetch the details using "Globals".

    to count the windows , you can use code like below.

     MessageBox.Show(Globals.ThisAddIn.Application.Windows.Count)

    to run the VBA macro , you can use code like below.

    Globals.ThisAddIn.Application.Run("ShowTableCleanup")

    in following video , you will see that first I try to display the window count and then call the macro that stored in that document.

    code:

     Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
    
            MessageBox.Show(Globals.ThisAddIn.Application.Windows.Count)
            Globals.ThisAddIn.Application.Run("ShowTableCleanup")
        End Sub

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 28, 2017 9:31 AM
    Moderator

All replies

  • Hi,

    I Will move your thread to Visual Studio Development >Visual Studio Tools for Office (VSTO) forum, if you have problems about visual studio IDE, please feel free to let me know.

    Best regards,

    Joyce


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 28, 2017 5:38 AM
  • Hi SimplyPaul,

    you can try to fetch the details using "Globals".

    to count the windows , you can use code like below.

     MessageBox.Show(Globals.ThisAddIn.Application.Windows.Count)

    to run the VBA macro , you can use code like below.

    Globals.ThisAddIn.Application.Run("ShowTableCleanup")

    in following video , you will see that first I try to display the window count and then call the macro that stored in that document.

    code:

     Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
    
            MessageBox.Show(Globals.ThisAddIn.Application.Windows.Count)
            Globals.ThisAddIn.Application.Run("ShowTableCleanup")
        End Sub

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 28, 2017 9:31 AM
    Moderator
  • Deepak,

    Thank you so much for your help.  Your solution worked perfectly.

    If I have more questions regarding Visual Studio or Add-Ins, should I start a new thread or just come back to this one again?

    Thanks again.

    Paul

    Thursday, November 30, 2017 1:24 AM
  • Apologies.  I'm new to Forums and Visual Studio.  I had no clue which option to choose.  But in the end I got my answer from Deepak.  Thank you all!
    Thursday, November 30, 2017 1:25 AM
  • Hi SimplyPaul,

    you had asked,"If I have more questions regarding Visual Studio or Add-Ins, should I start a new thread or just come back to this one again?"

    you need to start a new thread, for each new issue.

    we suggest you to create only one thread for one issue and don't continue the posting new issues on the same thread.

    it will be easy to understand and manage the threads. also it will help other community members in future who have same kind of issues.

    Thanks for your understanding.

    Regards

    Deepak 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 30, 2017 1:31 AM
    Moderator