diff --git a/CHANGES.txt b/CHANGES.txt index 0d513150c2..19ee965020 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -33,7 +33,7 @@ Coming in build 309, as yet unreleased * Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2332, #2330, @Avasam) * Fixed accidentally trying to raise an undefined name instead of an `Exception` in `Pythonwin/pywin/debugger/debugger.py` (#2326, @Avasam) * Fixed PythonService DoLogMessage raising fatal GIL lock error (#2426, JacobNolan1) -* Fixed and improved the following demos: `ddeclient`, `ddeserver`, `EvtSubscribe_push`, `openGLDemo`, `guidemo`, `ocxserialtest`, `ocxtest` (#2290, #2281, #2291, @Avasam) +* Fixed and improved the following demos: `ddeclient`, `ddeserver`, `EvtSubscribe_push`, `openGLDemo`, `guidemo`, `ocxserialtest`, `ocxtest`, `testMSOffice.TestWord8` (#2290, #2281, #2291, #2478 @Avasam) Build 308, released 2024-10-12 ------------------------------ diff --git a/com/win32com/test/testMSOffice.py b/com/win32com/test/testMSOffice.py index d3f93da7fc..355ce62523 100644 --- a/com/win32com/test/testMSOffice.py +++ b/com/win32com/test/testMSOffice.py @@ -17,30 +17,29 @@ # Test a few of the MSOffice components. def TestWord(): - # Try and load the object exposed by Word 8 - # Office 97 - _totally_ different object model! try: - # NOTE - using "client.Dispatch" would return an msword8.py instance! - print("Starting Word 8 for dynamic test") - word = win32com.client.dynamic.Dispatch("Word.Application") - TestWord8(word) - - word = None - # Now we will test Dispatch without the new "lazy" capabilities - print("Starting Word 8 for non-lazy dynamic test") - dispatch = win32com.client.dynamic._GetGoodDispatch("Word.Application") - typeinfo = dispatch.GetTypeInfo() - attr = typeinfo.GetTypeAttr() - olerepr = win32com.client.build.DispatchItem(typeinfo, attr, None, 0) - word = win32com.client.dynamic.CDispatch(dispatch, olerepr) - dispatch = typeinfo = attr = olerepr = None - TestWord8(word) - - except pythoncom.com_error: - print("Starting Word 7 for dynamic test") - word = win32com.client.Dispatch("Word.Basic") - TestWord7(word) - + # Office 97 - _totally_ different object model! + word7 = win32com.client.Dispatch("Word.Basic") + # Check if any property needed by TestWord7 is not None + if word7.FileNew: + print("Starting Word 7 for dynamic test") + TestWord7(word7) + else: + # NOTE - using "client.Dispatch" would return an msword8.py instance! + print("Starting Word 8 for dynamic test") + word = win32com.client.dynamic.Dispatch("Word.Application") + TestWord8(word) + + word = None + # Now we will test Dispatch without the new "lazy" capabilities + print("Starting Word 8 for non-lazy dynamic test") + dispatch = win32com.client.dynamic._GetGoodDispatch("Word.Application") + typeinfo = dispatch.GetTypeInfo() + attr = typeinfo.GetTypeAttr() + olerepr = win32com.client.build.DispatchItem(typeinfo, attr, None, 0) + word = win32com.client.dynamic.CDispatch(dispatch, olerepr) + dispatch = typeinfo = attr = olerepr = None + TestWord8(word) except Exception as e: print("Word dynamic tests failed", e) traceback.print_exc() @@ -74,24 +73,24 @@ def TestWord8(word): doc = word.Documents.Add() wrange = doc.Range() for i in range(10): - wrange.InsertAfter("Hello from Python %d\n" % i) + wrange.InsertAfter(f"Hello from Python {i + 1}\n") paras = doc.Paragraphs - for i in range(len(paras)): - # *sob* - in Word 2019, `p = paras(i+1)` seems to work to get a para - # but `p.Font` then blows up. - # p = paras[i]() - p = paras(i + 1) - p.Font.ColorIndex = i + 1 - p.Font.Size = 12 + (4 * i) - # XXX - note that - # for para in paras: - # para().Font... - # doesn't seem to work - no error, just doesn't work - # Should check if it works for VB! - doc.Close(SaveChanges=0) + if int(word.Version.split(".")[0]) >= 16: + # With Word 16 / Word 2019 + for i, p in enumerate(paras): + p.Range.Font.ColorIndex = i + 1 + p.Range.Font.Size = 12 + (4 * i) + else: + # NOTE: Iterating on paras doesn't seem to work - no error, just doesn't work + # for para in paras: + # para().Font... + for i in range(len(paras)): + p = paras(i + 1) + p.Font.ColorIndex = i + 1 + p.Font.Size = 12 + (4 * i) + doc.Close(SaveChanges=False) word.Quit() - win32api.Sleep(1000) # Wait for word to close, else we - # may get OA error. + win32api.Sleep(1000) # Wait for word to close, else we may get OA error. def TestWord8OldStyle(): @@ -168,7 +167,7 @@ def TestAll(): try: print("Starting Excel 8 for generated excel8.py test...") mod = gencache.EnsureModule( - "{00020813-0000-0000-C000-000000000046}", 0, 1, 2, bForDemand=1 + "{00020813-0000-0000-C000-000000000046}", 0, 1, 2, bForDemand=True ) xl = win32com.client.Dispatch("Excel.Application") TextExcel(xl)