如何将文本框锚定到word interop中的表格单元格 [英] How to anchor a text box to a table cell in word interop

查看:334
本文介绍了如何将文本框锚定到word interop中的表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#interop for Word,我可以创建一个包含5列和多行的表。我想在特定行的第5列中插入2个内容:表示百分比的数字和文本框。文本框的宽度等于百分比。文本框将被着色并且不包含任何文本。



我的问题是,当我创建文本框时,它始终锚定到文本框的第一列。行,而不是我请求的列。偏移量(.1)也是相对于第一列计算的。我需要将文本框固定到最后一列。



如果我手动编辑文档,我可以将文本框的锚点移动到右侧单元格,一切看起来都没问题。



这是代码。调用[cell]等于最后一个行中的单元格。该值是正确的,因为百分比的文本显示在该单元格中。



私有静态Word.Cell SetCellPercent(Word.Cell单元格,十进制目标,小数需要,bool?粗体)

{

十进制pct;

浮动pctfloat,wid,hei;

Word.Shape框;

Word.Range rng;



pct = CalculatePercentDone(目标,需要);

SetCell(cell,(pct * 100).ToString(N0),null,bold); //将文本存储到单元格并应用属性



pctfloat = Convert.ToSingle(pct);

wid = BOX_WIDTH * PPI * pctfloat ; PPI =每英寸点数= 72

hei = BOX_HEIGHT * PPI;

rng = cell.Range;



box = cell.Range.Document.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizo​​ntal,.1f * PPI,0.1f * PPI,wid,hei,rng);

return单元格;

}



我尝试过:



我为[rng]尝试了其他值,比如将其设置为单元格中第一个和最后一个段落的范围。相同的结果。

解决方案

此添加导致文本框的锚点位于指定的单元格中:



之后设置变量[rng],添加此行。似乎工作正常,虽然它的确切原因对我来说仍然是一个谜。



rng.Collapse(Word.WdCollapseDirection.wdCollapseStart);



请参阅https://social.msdn.microsoft.com/Forums/vstudio/en-US/88626c3f-1b22-46d4-8330-450c616b11bc/word-2007-insert-autoshape-在表小区?论坛= VSTO

Using C# interop for Word, I can create a table with 5 columns and several rows. I want to insert 2 things into column 5 of a particular row: a number representing a percentage, and a text box. The text box's width is equivalent to the percentage. The text box will be colored and will not contain any text.

My problem is that when I create the text box, it is always anchored to the first column of the row, not the column I request. The offsets (.1") also are calculated relative to the first column. I need to have the text box anchored to the last column.

If I manually edit the document, I can move the text box's anchor to the right cell, and everything looks OK.

Here's the code. It is invoked with [cell] equal to the last cell in the row. That value is correct because the text of the percentage shows up in that cell.

private static Word.Cell SetCellPercent(Word.Cell cell, decimal goal, decimal needed, bool? bold)
{
decimal pct;
float pctfloat, wid, hei;
Word.Shape box;
Word.Range rng;

pct = CalculatePercentDone(goal, needed);
SetCell(cell, (pct * 100).ToString("N0"), null, bold); // Store text into cell and apply attributes

pctfloat = Convert.ToSingle(pct);
wid = BOX_WIDTH * PPI * pctfloat; PPI = Points Per Inch = 72
hei = BOX_HEIGHT * PPI;
rng = cell.Range;

box = cell.Range.Document.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, .1f * PPI, 0.1f * PPI, wid, hei, rng);
return cell;
}

What I have tried:

I tried other values for [rng], like setting it to the range of the first and last paragraphs in the cell. Same result.

解决方案

This addition causes the textbox's anchor to be in the designated cell:

After setting variable [rng], add this line. Seems to work fine, although the precise reason for it is still a mystery to me.

rng.Collapse(Word.WdCollapseDirection.wdCollapseStart);

See https://social.msdn.microsoft.com/Forums/vstudio/en-US/88626c3f-1b22-46d4-8330-450c616b11bc/word-2007-insert-autoshape-in-table-cell?forum=vsto


这篇关于如何将文本框锚定到word interop中的表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆