python win32com vba用户形对象字幕属性集

发布于 2025-02-03 05:56:25 字数 651 浏览 1 评论 0原文

我正在尝试使用Python中的Win32COM创建Excel VBA用户形式。我能够将用户形式添加到工作簿中,甚至可以将控件添加到此表单中,但是我坚持更新用户形象对象本身的属性。例如:我可以使用以下代码阅读字幕属性,但是我没有运气设置它。 有人知道如何使用Win32COM在Python中设置VBA用户形式属性吗?

我的代码:

import win32com.client as win32

com_object = win32.gencache.EnsureDispatch('Excel.Application')
xlFile = com_object.Workbooks.Open(Filename = 'MyExcelFileaPath.xlsx')
xlForm = xlFile.VBProject.VBComponents.Add(3) 
print (xlForm.Properties('Caption')) #this works
xlForm.Properties('Caption') = 'Custom Caption' #this does not work - error: SyntaxError: can't assign to function call
xlForm.Properties.Caption = 'Custom Caption' #this does not work

I am trying to create Excel VBA UserForm using Win32com in Python. I am able to add the UserForm to a workbook, I can even add controls to this form, however I am stuck with updating the properties of the UserForm object itself. For example: I can read the Caption property using the below code, but I had no luck setting it.
Does anyone know how to set VBA UserForm properties in Python using win32com?

My Code:

import win32com.client as win32

com_object = win32.gencache.EnsureDispatch('Excel.Application')
xlFile = com_object.Workbooks.Open(Filename = 'MyExcelFileaPath.xlsx')
xlForm = xlFile.VBProject.VBComponents.Add(3) 
print (xlForm.Properties('Caption')) #this works
xlForm.Properties('Caption') = 'Custom Caption' #this does not work - error: SyntaxError: can't assign to function call
xlForm.Properties.Caption = 'Custom Caption' #this does not work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2025-02-10 05:56:25

最后,经过一周的研究,反复试验,我想出了一种设置用户形象对象属性的方法。正如我所怀疑的那样,答案有点愚蠢:

xlForm.Properties('Caption').Value = 'Custom Caption'

希望这可以帮助某人节省一些时间。

Finally, after a week of research, trial and error I came up with a way to set UserForm object's properties. As I suspected, the answer is kind of silly:

xlForm.Properties('Caption').Value = 'Custom Caption'

Hope this helps someone to save some time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文