开发者

Interacting with Word via .NET

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.Intero开发者_如何学Gop.Word.WdUnits.wdLine, 1, Microsoft.Office.Interop.Word.WdMovementType.wdExtend);


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜