(MATLAB)ActiveX - > VBA - >填充/逐行着色MS Word表的最快方法?

时间:2012-02-17 06:21:52

标签: activex word-vba

在ActiveX上创建,填充和着色MS Word表格的最快方法是什么?> VBA(在我的特殊情况下,我是通过MATLAB完成的)?

现在我正在以下列方式使用它

hdlActiveX = actxserver('Word.Application');
...
hdlActiveX.ActiveDocument.Tables.Add(hdlActiveX.Selection.Range, nbRows, nbCols, 1, 1).Rows.Alignment = 1;
...
% create dataCell{nbRows, nbCols}  containing the table values row- and columnwise
...
nrTable = ... % an integer selecting a table in the MS Word document 
...
for r = 1:nbRows
   for c = 1:nbCols
       % Write data into current cell
      hdlActiveX.ActiveDocument.Tables.Item(1).Cell(r,c).Range.Text = dataCell{r, c};

      % setting a different background color rowwise, if a condtion is met
      % this is even bigger performance reducer
      if condition
          for iCol = 1:nbCols
              hdlActiveX.ActiveDocument.Tables.Item(nrTable).Cell(r,iCol).Select;
              hdlActiveX.Selection.Shading.BackgroundPatternColor = color;
          end
      end

   end
end

但是需要很长时间才能填满,特别是要逐行填充MS Word(ActiveX)表格......那么填写表格的性能如何得到改善?

提前致谢,

2 个答案:

答案 0 :(得分:0)

我可以用非常简单的策略解决上述问题:

1) create a tab-delimitered text file with the table entries (simulate the table)
2) open over activex the text file, and copy its contents
3) create the table rows/cows
4) paste the contents of the whole table.

这解决了填充问题。

背景着色问题可能会超过预定义的表格格式以及行方向选择和着色(不是单元格)。

此致

答案 1 :(得分:0)

我正在寻找自己的答案。

使应用程序单词隐身

wordApp.Visible = 0;                
wordApp.ScreenUpdating = 0;

将我的文档创建时间从50分钟缩短(真的......有很多表格)到13。