yxh1202 发表于 2010-6-8 11:47:00

VBA代码的奇怪错误在哪

<p><font face="Verdana">我编写了段代码,想把EXCEL中的文字提取到CAD中,并设为多行文字,对齐方式为正中,但是奇怪的是执行后,最后一列(设计渠底)字高个对齐方式没有变,烦请高人指点啊</font></p>
<p><font face="Verdana"></font>&nbsp;</p>
<p><font face="Verdana">Sub uu()<br/>Dim insPnt(0 To 2) As Double<br/>&nbsp;On Error Resume Next&nbsp; '这句话很重要<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 连接Excel应用程序<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim xlApp As Excel.Application<br/>&nbsp;&nbsp;&nbsp;&nbsp; Set xlApp = GetObject(, "Excel.Application")<br/>&nbsp;&nbsp;&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox " Excel 应用程序没有运行。请启动 Excel 并重新运行程序。"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<br/>&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp; Dim xlSheet As Worksheet<br/>&nbsp;&nbsp;&nbsp;&nbsp; Set xlSheet = xlApp.ActiveSheet<br/>zg = 2.5<br/>For p = 1 To 6<br/>&nbsp;&nbsp;&nbsp;&nbsp; txtStr = xlSheet.Cells(1, p)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtHeight = 25<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insPnt(0) = 100: insPnt(1) = 100 - p * 15 + 8.5: insPnt(2) = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mtextObj.AttachmentPoint = 5<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mtextObj.height = 2 * zg<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set mtextObj = ThisDrawing.ModelSpace.AddMText(insPnt, txtHeight, txtStr)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next p<br/>End Sub</font></p>
<p>&nbsp;</p>
<p>电子表格里的第一行是如下内容</p>
<p>&nbsp;</p>
<p>
<table style="WIDTH: 324pt; BORDER-COLLAPSE: collapse" border="0" cellspacing="0" cellpadding="0" width="432" x:str>
<colgroup>
<col style="WIDTH: 54pt" span="6" width="72">
<tbody>
<tr style="HEIGHT: 14.25pt" height="19">
<td style="WIDTH: 54pt; HEIGHT: 14.25pt" class="xl22" height="19" width="72">
<p>桩号</p>
<p>&nbsp;</p></td>
<td style="BORDER-LEFT: medium="medium"none; WIDTH: 54pt" class="xl22" width="72">现状渠顶</td>
<td style="BORDER-LEFT: medium="medium"none; WIDTH: 54pt" class="xl22" width="72">设计渠顶</td>
<td style="BORDER-LEFT: medium="medium"none; WIDTH: 54pt" class="xl22" width="72">设计水位</td>
<td style="BORDER-LEFT: medium="medium"none; WIDTH: 54pt" class="xl22" width="72">现状渠底</td>
<td style="BORDER-LEFT: medium="medium"none; WIDTH: 54pt" class="xl22" width="72">设计渠底</td></tr>
<tr style="HEIGHT: 14.25pt" height="19">
<td style="HEIGHT: 14.25pt; BORDER-TOP: medium="medium"none" class="xl22" height="19"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td></tr>
<tr style="HEIGHT: 14.25pt" height="19">
<td style="HEIGHT: 14.25pt; BORDER-TOP: medium="medium"none" class="xl22" height="19"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td></tr>
<tr style="HEIGHT: 14.25pt" height="19">
<td style="HEIGHT: 14.25pt; BORDER-TOP: medium="medium"none" class="xl22" height="19"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td></tr>
<tr style="HEIGHT: 14.25pt" height="19">
<td style="HEIGHT: 14.25pt; BORDER-TOP: medium="medium"none" class="xl22" height="19"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td></tr>
<tr style="HEIGHT: 14.25pt" height="19">
<td style="HEIGHT: 14.25pt; BORDER-TOP: medium="medium"none" class="xl22" height="19"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td>
<td style="BORDER-LEFT: medium="medium"none; BORDER-TOP: medium="medium"none" class="xl22"> </td></tr></tbody></table></p>
<p>&nbsp;</p>
<p>&nbsp;</p>

woaishuijia 发表于 2010-6-8 19:02:00

<p>For p = 1 To 6<br/>&nbsp;&nbsp;&nbsp;&nbsp; txtStr = xlSheet.Cells(1, p)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtHeight = 2 * zg<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insPnt(0) = 100: insPnt(1) = 100 - p * 15 + 8.5: insPnt(2) = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set mtextObj = ThisDrawing.ModelSpace.AddMText(insPnt, txtHeight, txtStr)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mtextObj.AttachmentPoint = 5</p>
<p>Next p</p>

yxh1202 发表于 2010-6-9 15:12:00

谢谢,就是把字高属性语句放到最后。可是为何放在前面就出现最后一行没有执行字高呢。百思不得其解。不过还是谢谢你啊

woaishuijia 发表于 2010-6-9 16:06:00

<p>按你的代码,进入FOR循环时,先设置mtextObj的对正和字高属性,可这时mtextObj对象还没有创建,mtextObj还不存在,这时修改其属性的两行语句一定会出错.但你在代码的最前面有一行On Error Resume Next,尽管你的本意是用这个错误处理代码来检测EXCEL程序是否运行,但到了循环语句时,这一句还在发挥作用,程序不会报错而是越过出错的两行代码向下运行到创建mtextObj这一行.创建一个mtextObj对象后程序又回到循环体的前面,设置mtextObj的对正和字高属性,这次不会出错了,但这次被设置属性的是第一次创建的mtextObj对象,然后再创建第二个mtextObj,然后再回去......直到创建最后一个mtextObj后直接退出循环.最后一个mtextObj并没有修改属性.</p>
<p>如果你在进入循环体之前,加一行On Error Goto 0,让前面的On Error Resume Next失效,你就会发现修改属性的两行放在这里是错误的.</p>
<p>所以我对你的代码做的修改主要就是先创建对象,再修改属性,仅此而已.</p>
<p>另外,你在创建对象时使用的字高和后面修改的字高不一致,好像没有必要吧?所以对此也一并做了修改,在创建对象时直接按要求指定字高,而不再修改字高属性.</p>
<p>&nbsp;</p>

yxh1202 发表于 2010-6-9 16:40:00

<p>谢谢楼上的帮助。</p>
<p>但是addmext(坐标,多行文字宽度,文字)</p>
<p>我定义txtheight=25是为了不重新定义变量</p>
<p>&nbsp;</p>
<p>Set mtextObj = ThisDrawing.ModelSpace.AddMText(insPnt, txtHeight, txtStr)</p>
<p>&nbsp;</p>
<p>mtextObj.height = 2 * zg&nbsp; ‘这一句才是为多行文字赋字高属性啊。</p>
<p>不知道对不对,请多指教<br/><br/></p>

woaishuijia 发表于 2010-6-10 20:25:00

<p>对不起,是我搞错了.呵呵,我把单行文字和多行文字搞混了.</p>
<p>你是对的,为多行文字赋予字高应该有.</p>

luna125 发表于 2010-10-22 10:10:00

取之于明 用之于明&nbsp;<span class="m_li_top"style="DISPLAY: inline"><br/></span>
页: [1]
查看完整版本: VBA代码的奇怪错误在哪