none
How to use the ActiveDocument from an Word AddIn?

    Question

  • Hi,

    i want to develop a Word 2007 AddIn. What i have done yet: (Everything with Visual Studio 2008 and VSTO)

    1. Create a new Word 2007 Add-In
    2. Insert a Ribbon with an own Button (btnOpenForm)
    3. Create a new Windows Form (frmMainData)
    4. Insert onClick Event for btnOpenForm
    5. Insert cmd for Show.Dialog for frmMainData

    In this Form i want to click on a Button and read a built in Bookmark or set this Bookmark of an ACTUALLY OPEN DOCUMENT. Cause after i start the debugger, Word opens automatically. Then i open a document with the default word -> open file  menue. When i now klick and start my addin then i want to be able to use the active document.

    For Reading a Bookmark i tried the following:

    Private Sub GetProperties ()
    
    dim w as new word.application
    dim document as word.document
    
    document = w.activedocument
    
    if document.bookmarks.exists("MyBookmark") then
        msgbox "Ok"
    else
       msgbox "not ok"
    End If
    
    End SUb
    
    When i dbug my AddIn and open a new Word Document and try to get the bookmark or check if exists then i get the error:

    "document = w.activedocument" -> The Command is not availabel, cause there is no Document open.

    What wrong?

    Can someone help? Thank you

    Yavuz
    Thursday, July 23, 2009 7:47 PM

Answers

  • Ok i have it:

    set wapp as microsoft.office.interop.word.application = globals.thisaddin.application
    set doc as  microsoft.office.interop.word.document = wapp.activedocument

    ...
    thank you
    • Marked as answer by Yavuz Bogazci Friday, July 24, 2009 4:04 PM
    Friday, July 24, 2009 4:04 PM

All replies

  • Hi Yavuz

    Try changing your code to below:

    Set doc = w.activedocument

    and the 'document' variable is a keyword of VBA/Word.

    you should change the name of variable such as doc

    SJOO



    • Edited by SJOO Friday, July 24, 2009 8:03 AM
    • Marked as answer by Yavuz Bogazci Friday, July 24, 2009 4:02 PM
    • Unmarked as answer by Yavuz Bogazci Friday, July 24, 2009 4:03 PM
    Friday, July 24, 2009 7:59 AM
  • Ok i have it:

    set wapp as microsoft.office.interop.word.application = globals.thisaddin.application
    set doc as  microsoft.office.interop.word.document = wapp.activedocument

    ...
    thank you
    • Marked as answer by Yavuz Bogazci Friday, July 24, 2009 4:04 PM
    Friday, July 24, 2009 4:04 PM