0

I am running this example of a RTD server.

I make the call in excel:

 =RTD("Python.RTD.TimeServer","","seconds","5") 

The first call works, but than it doesn't update anymore.

There is an error:

pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

It happens when the excel callback is called:

self.__callback.UpdateNotify()

I am not an expert on the topic so my hypotheses are mostly guessing.

One thing I found is that in the beginning when you define the Excel type lib:

EXCEL_TLB_GUID = '{00020813-0000-0000-C000-000000000046}'
EXCEL_TLB_LCID = 0
EXCEL_TLB_MAJOR = 1
EXCEL_TLB_MINOR = 4

# Import the excel typelib to make sure we've got early-binding going on. 
# The "ByRef" parameters we use later won't work without this.
gencache.EnsureModule(EXCEL_TLB_GUID, EXCEL_TLB_LCID, \
                      EXCEL_TLB_MAJOR, EXCEL_TLB_MINOR)

The Guid is not the same as of my excel application which is {00024500-0000-0000-C000-000000000046}.

I have tried, however than I get an error earlier in the EnsureModule function:

    tlb = pythoncom.LoadRegTypeLib(typelibCLSID, major, minor, lcid)

pywintypes.com_error: (-2147319779, 'Library not registered.', None, None)

I saw someone manipulated the other variables for this call, but I don't even know what they are.

I have 64-bit Python and 64-bit Excel.

The Output from HKEY_LOCAL_MACHINE\software\classes\CLSID{00020830-0000-0000-C000-000000000046}:

 Hive: HKEY_LOCAL_MACHINE\software\classes\CLSID\{00020830-0000-0000-C000-000000000046}


Name                           Property
----                           --------
AuxUserType
Conversion
DataFormats
DefaultExtension               (default) : .xlsx, Excel Workbook (*.xlsx)
DefaultIcon                    (default) : C:\Program Files\Microsoft

                               Office\Root\VFS\Windows\Installer\{90160000-000F-0000-1000-0000000FF1CE}\xlicons.exe,1
DocObject                      (default) : 16
Implemented Categories
InprocHandler32                (default) : ole32.dll
Insertable                     (default) :
LocalServer32                  (default) : C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE
MiscStatus                     (default) : 0
ProgID                         (default) : Excel.Sheet.12
TypeLib                        (default) : {00020813-0000-0000-C000-000000000046}
Verb
Version                        (default) : 1.6
VersionIndependentProgID       (default) : Excel.Sheet
Borut Flis
  • 15,715
  • 30
  • 92
  • 119
  • 1
    You can use PowerShell to explore registry. `cd hklm:\software\classes\excel.sheet`, then `ls`. Will list a prop that is CurVer. Probably "Excel.Sheet.12". If so `cd ..\Excel.Sheet.12`. Then `ls` and look for CLSID. Then `cd "..\CLSID\{00020830-0000-0000-C000-000000000046}"` probably ... if your clsid is different then use that. Again `ls` and look for TypeLib property. That is your type library GUID and will be different than Excel application GUID. Use it like `cd "..\..\{00020813-0000-0000-C000-000000000046}"`. And again, `ls`. On my system I see "1.9". That is the major.minor version. – Joseph Willcoxson Mar 25 '21 at 15:10
  • 1
    You could also open regedit.exe and navigate to HKEY_CLASSES_ROOT\TypeLib and from there search (Ctrl+F or whatever it is in your locale) for "excel.exe". The TypeLib is embedded in Excel.exe (other Office products like Word have external typelibs in separate files). When you find it, the tree will expand and it will show you the CLSID of the typelib and also the major.minor verson. – Joseph Willcoxson Mar 25 '21 at 15:18
  • Ok, so I followed you second comment. The CLSID is {00020813-0000-0000-C000-000000000046}, like specified in the server code, however there is no mention of the minor, major. Furthemore, do you know what is the LCID? Because this value is called in the line where the error occurs. – Borut Flis Mar 27 '21 at 19:42
  • LCID probably 0. It’s the subkey of the version. There is the guid of the type library, it’s child is the version, maybe 1.9. Next subchild is the LCID—likely zero, and next subchild is Win64 or Win32. – Joseph Willcoxson Mar 28 '21 at 05:53
  • Hey, I added the input what you instructed in your first coment. So the version 1.6 are the values for major/minor if I understand correctly?? – Borut Flis Mar 28 '21 at 18:58
  • Does the the type lib enable early binding? – Borut Flis Mar 28 '21 at 19:00
  • 1
    Have no idea how Python handles the type library. It might enable early binding. Really, I prefer late when working with Office. The "cost" when calling Office is the marshaling. Usually I prefer late binding because I've had bad experience with Office registering (or not registering) types and especially interfaces--or old interfaces no longer being registered. More often than not, the extra overhead for late binding is not that significant. – Joseph Willcoxson Mar 29 '21 at 01:21
  • Hi, @BorutFlis have you found a working solution for this. If yes, please post the same. Thank you. – Krishna Kumar Apr 24 '21 at 11:11
  • @KrishnaKumar No I haven't been successful yet, I opened a new question about this problem, still no success. Do you get the same error? Do you want to talk about it and exchange information? – Borut Flis Apr 26 '21 at 13:56
  • @BorutFlis I am looking for a working solution in order to implement a python RTD server. However, after seeing your post I haven't tried yet. Have you tried [xlOil](https://xloil.readthedocs.io/en/latest/xlOil_Python/index.html) ? – Krishna Kumar Apr 28 '21 at 09:48
  • @KrishnaKumar It might be that it works for you. I believe the code will work if I am able to run the object as InProc. – Borut Flis Apr 30 '21 at 10:14
  • @BorutFlis I am not able to register pythonRTD server code [example](https://github.com/SublimeText/Pywin32/blob/master/lib/x32/win32com/demos/excelRTDServer.py). I am getting the following error. `TypeError: Can't locate the script hosting the COM object - please set _reg_class_spec_ in your object`. Can you help? – Krishna Kumar Apr 30 '21 at 14:48
  • Interesting, in my it says _reg_classspec_ is "An optional string identifying the Python module and the object in the module." – Borut Flis Apr 30 '21 at 19:08
  • @BorutFlis I am able to register the com object. But when I do `=RTD("python.rtd.timeserver","","seconds","5")` in excel, I am getting `#N/A` in excel. – Krishna Kumar May 03 '21 at 09:20
  • The excel and the Python have to be the same bit. – Borut Flis May 11 '21 at 22:32
  • Also I am using xlwings now. It doesn't have rtd_server but you can change value of excel cells dynamically. – Borut Flis May 11 '21 at 22:32

0 Answers0