最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Microsoft.Office.Interop.Word Documents.Open not working with .Net 8? - Stack Overflow

programmeradmin4浏览0评论

Working on testing our pyRevit scripts for Revit 2025 which updated to .NET 8 and I'm getting an error in a script that works in previous versions. Also worth nothing that I'm relatively new to coding.

Here is the code:

import Microsoft.Office.Interop.Word as Word
from pyrevit import DB, revit, script, forms, coreutils

# Word Test
missing = Missing
word_application = Word.ApplicationClass()
word_application.Visible = False

# Forms
wordDoc = forms.pick_file(file_ext="docx",init_dir="J:",
                            multi_file = False, title = "Pick a .docx word document.")

document = word_application.Documents.Open(wordDoc, missing, True)

This is the error.

IronPython Traceback:
Traceback (most recent call last):
 File "L:\Revit\Development\PyRevit\DevTool\DevTool.extension\DevTools.tab\Development.Panel\SheetSpec.pushbutton\SheetSpec_script.py", line 69, in <module>
AttributeError: '__ComObject' object has no attribute 'Open'

I've tried printing the methods for Documents, and I'm getting a list that does not match the documentation. I feel like there is something I fundamentally do not understand about what changed with the .NET 8 update.

print(dir(word_application.Documents))
<System.__ComObject object at 0x00000000000009F3 [System.__ComObject]>



['Equals', 'GetHashCode', 'GetLifetimeService', 'GetType', 'InitializeLifetimeService', 'MemberwiseClone', 'ReferenceEquals', 'ToString', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

This is the documentation, .office.interop.word.documents?view=word-pia

When I run the same script in Revit 2023 and Revit 2024 it prints this:

['Add', 'AddBlogDocument', 'AddOld', 'Application', 'CanCheckOut', 'CheckOut', 'Close', 'Count', 'CreateObjRef', 'Creator', 'Equals', 'GetHashCode', 'GetLifetimeService', 'GetType', 'InitializeLifetimeService', 'Item', 'MemberwiseClone', 'Open', 'Open2000', 'Open2002', 'OpenNoRepairDialog', 'OpenOld', 'Parent', 'ReferenceEquals', 'Save', 'ToString', '_NewEnum', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
发布评论

评论列表(0)

  1. 暂无评论