I tried to create a really basic Word AddIn based on the Excel example
(which works fine for me).  I've pasted the code below. 

 

Apologies for my general ignorance - I've trolled the Archives looking for
something close but no dice.  I've had luck making an Excel AddIns in the
past and can make my way round the Word Object model, VBA et al. But I'm
really winging it with the COM stuff.

 

Thanks in advance to who-ever takes a look at this .

 

Kieran Martin 

e-Spatial Ltd

New Zealand

 

 

Here's the trace collector data:

 

# This window will display output from any programs that import
win32traceutil

# win32com servers registered with '--debug' are in this category.

Object with win32trace dispatcher created (object=None)

OnConnection Microsoft Word 0 A Simple Word Addin (2,)

pythoncom error: Failed to call the universal dispatcher

 

Traceback (most recent call last):

  File "C:\Python24\lib\site-packages\win32com\universal.py", line 177, in
dispatch

    retVal = ob._InvokeEx_(meth.dispid, 0, meth.invkind, args, None, None)

  File "C:\Python24\lib\site-packages\win32com\server\policy.py", line 332,
in _InvokeEx_

    return self._invokeex_(dispid, lcid, wFlags, args, kwargs,
serviceProvider)

  File "C:\Python24\lib\site-packages\win32com\server\policy.py", line 654,
in _invokeex_

    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags,
args, kwArgs, serviceProvider)

  File "C:\Python24\lib\site-packages\win32com\server\policy.py", line 588,
in _invokeex_

    return func(*args)

  File "c:\Python24\Lib\site-packages\win32com\demos\wordAddin.py", line 62,
in OnConnection

    print "OnDisconnection"

exceptions.NameError: global name 'appHostApp' is not defined

pythoncom error: Unexpected gateway error

 

Traceback (most recent call last):

  File "C:\Python24\lib\site-packages\win32com\universal.py", line 177, in
dispatch

    retVal = ob._InvokeEx_(meth.dispid, 0, meth.invkind, args, None, None)

  File "C:\Python24\lib\site-packages\win32com\server\policy.py", line 332,
in _InvokeEx_

    return self._invokeex_(dispid, lcid, wFlags, args, kwargs,
serviceProvider)

  File "C:\Python24\lib\site-packages\win32com\server\policy.py", line 654,
in _invokeex_

    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags,
args, kwArgs, serviceProvider)

  File "C:\Python24\lib\site-packages\win32com\server\policy.py", line 588,
in _invokeex_

    return func(*args)

  File "c:\Python24\Lib\site-packages\win32com\demos\wordAddin.py", line 62,
in OnConnection

    print "OnDisconnection"

exceptions.NameError: global name 'appHostApp' is not defined

 

 

>>>> here's the code

 

 

 

#

from win32com import universal

from win32com.server.exception import COMException

from win32com.client import gencache, DispatchWithEvents

import winerror

import pythoncom

from win32com.client import constants, Dispatch

import sys

 

# Support for COM objects we use.

gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 1, 3,
bForDemand=True) # Word 11

gencache.EnsureModule('{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}', 0, 2, 1,
bForDemand=True) # Office 11

 

# The TLB defiining the interfaces we implement

universal.RegisterInterfaces('{AC0714F2-3D04-11D1-AE7D-00A0C90F26F4}', 0, 1,
0, ["_IDTExtensibility2"])

class ButtonEvent:

    def OnClick(self, button, cancel):

        import win32ui # Possible, but not necessary, to use a Pythonwin GUI

        import win32con

        win32ui.MessageBox("Hello from Python", "Python
Test",win32con.MB_OKCANCEL)

        return cancel

 

class WordAddin:

    _com_interfaces_ = ['_IDTExtensibility2']

    _public_methods_ = []

    _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER

    _reg_clsid_ = "{C5482ECA-F559-45A0-B078-B2036E6F011A}"

    _reg_progid_ = "Python.Test.WordAddin"

    _reg_policy_spec_ = "win32com.server.policy.EventHandlerPolicy"

 

    def __init__(self):

        return

    

    def OnConnection(self, application, connectMode, addin, custom):

        print "OnConnection", application, connectMode, addin, custom

 

    def OnDisconnection(self, mode, custom):

        print "OnDisconnection"

        

    def OnAddInsUpdate(self, custom):

        print "OnAddInsUpdate", custom

    def OnStartupComplete(self, custom):

        print "OnStartupComplete", custom

    def OnBeginShutdown(self, custom):

        print "OnBeginShutdown", custom

 

def RegisterAddin(klass):

    import _winreg

    key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER,
"Software\\Microsoft\\Office\\Word\\Addins")

    subkey = _winreg.CreateKey(key, klass._reg_progid_)

    _winreg.SetValueEx(subkey, "CommandLineSafe", 0, _winreg.REG_DWORD, 0)

    _winreg.SetValueEx(subkey, "LoadBehavior", 0, _winreg.REG_DWORD, 3)

    _winreg.SetValueEx(subkey, "Description", 0, _winreg.REG_SZ, "Word
Addin")

    _winreg.SetValueEx(subkey, "FriendlyName", 0, _winreg.REG_SZ, "A Simple
Word Addin")

 

def UnregisterAddin(klass):

    import _winreg

    try:

        _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER,
"Software\\Microsoft\\Office\\Word\\Addins\\" + klass._reg_progid_)

    except WindowsError:

        pass

 

if __name__ == '__main__':

    import win32com.server.register

    win32com.server.register.UseCommandLine(WordAddin)

    if "--unregister" in sys.argv:

        UnregisterAddin(WordAddin)

    else:

        RegisterAddin(WordAddin)

 

 

 

 

 

 

_______________________________________________________ 
Kieran Martin 
e-Spatial Limited                                PO Box 10015 
Phone: + 64 4 499 3546                    Wellington 6143
Fax:      + 64 4 499 3547                    NEW ZEALAND 

Cell:      + 64 274 9000 77
                           <http://www.e-spatial.co.nz/> www.e-spatial.co.nz

      location based business intelligence

 

_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to