通过 .NET 与 Word 交互

发布于 2024-11-19 07:07:46 字数 2423 浏览 2 评论 0原文

经过几分钟的实验,我回答了我自己的问题(见下文)。当 SO 在 2 天内允许我时,我会接受这个答案。


我正在使用 MATLAB 的 .NET 功能来创建 Word 文档。我想创建一个表格并合并左侧列中顶部的两个单元格。到目前为止,我可以创建表格:

NET.addAssembly('microsoft.office.interop.word');         %# Register Word assembly
wordApp = Microsoft.Office.Interop.Word.ApplicationClass; %# Create an instance of Word
wordDoc = wordApp.Documents;                              %# Get the object that handles documents
newDoc = wordDoc.Add;                                     %# Add a new document
wordApp.Visible = 1;                                      %# Make Word visible
selection = wordApp.Selection;                            %# Get the selection object to manage selected area
table = newDoc.Tables.Add(selection.Range, 3, 5);         %# Create a table
table.Style = 'Table Grid';                               %# Add grid lines

我在调用 selection.MoveDown 方法时遇到问题。在此方法上运行 METHODSVIEW 指示以下输入参数:

  • Microsoft.Office。 Word.Selection 此
  • System.Object 单位
  • System.Object 计数
  • System.Object 扩展

MSDN 上的 Selection.MoveDown 文档表明,Unit 参数是一个 WdUnits 枚举,我在 MATLAB 的 Microsoft.Office.Interop.Word.WdUnits.wdCell 中找到了该枚举。

我的问题是我找不到 Extend 参数所需的 WdMovementType 枚举。我想使用 wdExtend 值,但找不到它。根据 MSDN 它应该位于 Microsoft.Office.Interop.Word.WdMovementType。 谁能告诉我在哪里可以找到 wdExtend?

编辑

WdMovementType 事实上确实存在于它应该存在的地方,即 Microsoft.Office.Interop.Word.WdMovementType。现在,我在尝试运行 MoveDown 方法时遇到错误:

>> selection.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdCell, 1, Microsoft.Office.Interop.Word.WdMovementType.wdExtend);
??? Message: Bad parameter
Source: Microsoft Word
HelpLink: C:\Program Files (x86)\Microsoft Office\Office12\1033\WDMAIN11.CHM#36888

知道如何正确调用此方法吗?

编辑

对 MoveDown 的调用应使用单位的 wdLine 值:

>> selection.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdLine, 1, Microsoft.Office.Interop.Word.WdMovementType.wdExtend);

After a few more minutes of experimenting, I answered my own question (see below). I'll accept that answer when SO lets me in 2 days.


I am using MATLAB's .NET functionality to create a Word document. I'd like to create a table and merge the top two cells in the lefthand column. So far I can create the table:

NET.addAssembly('microsoft.office.interop.word');         %# Register Word assembly
wordApp = Microsoft.Office.Interop.Word.ApplicationClass; %# Create an instance of Word
wordDoc = wordApp.Documents;                              %# Get the object that handles documents
newDoc = wordDoc.Add;                                     %# Add a new document
wordApp.Visible = 1;                                      %# Make Word visible
selection = wordApp.Selection;                            %# Get the selection object to manage selected area
table = newDoc.Tables.Add(selection.Range, 3, 5);         %# Create a table
table.Style = 'Table Grid';                               %# Add grid lines

I'm having trouble calling the selection.MoveDown method. Running METHODSVIEW on this method indicates the following input parameters:

  • Microsoft.Office.Word.Selection this
  • System.Object Unit
  • System.Object Count
  • System.Object Extend

The Selection.MoveDown documentation on MSDN suggests that the Unit parameter is a WdUnits enumeration which I found in MATLAB at Microsoft.Office.Interop.Word.WdUnits.wdCell.

My problem is that I can't find the WdMovementType enumeration needed by the Extend parameter. I want to use the wdExtend value but I can't find it. According to MSDN it should be at Microsoft.Office.Interop.Word.WdMovementType. Can anyone tell me where to find wdExtend?

EDIT

WdMovementType does in fact live where it's supposed to, i.e. Microsoft.Office.Interop.Word.WdMovementType. Now I'm getting an error when trying to run the MoveDown method:

>> selection.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdCell, 1, Microsoft.Office.Interop.Word.WdMovementType.wdExtend);
??? Message: Bad parameter
Source: Microsoft Word
HelpLink: C:\Program Files (x86)\Microsoft Office\Office12\1033\WDMAIN11.CHM#36888

Any idea how to call this method properly?

EDIT

The call to MoveDown should use the wdLine value for the units:

>> selection.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdLine, 1, Microsoft.Office.Interop.Word.WdMovementType.wdExtend);

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

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

发布评论

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

评论(2

风透绣罗衣 2024-11-26 07:07:46

我使用以下命令来检查 Word 程序集中的枚举:

>> word = NET.addAssembly('microsoft.office.interop.word');
>> word.Enums

结果包括:

 'Microsoft.Office.Interop.Word.WdMovementType'

因此我想尝试访问它,即使它没有出现在 MATLAB 的制表符补全中。奇怪的是,访问一次后,它现在出现了!我猜它一直都在那里。

I used the following to check the enumerations in the Word assembly:

>> word = NET.addAssembly('microsoft.office.interop.word');
>> word.Enums

The results included:

 'Microsoft.Office.Interop.Word.WdMovementType'

so I thought I'd try accessing it even though it wasn't appearing in MATLAB's tab completion. Strangely enough, after accessing it once it now appears! I guess it was there all along.

给我一枪 2024-11-26 07:07:46

我建议您下载Word 2010的最新程序集。
您可以从 microsoft.com 下载它们
如何:安装 Office 主互操作程序集

I suggest you to download the latest one assembly for the Word 2010.
You can download them from microsoft.com
How to: Install Office Primary Interop Assemblies

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