如何用vba调整word表格的行高、列宽?

您所在的位置:网站首页 word表格高度 如何用vba调整word表格的行高、列宽?

如何用vba调整word表格的行高、列宽?

2023-12-27 22:16| 来源: 网络整理| 查看: 265

如何用vba调整word表格的行高、列宽? 2020年3月14日 作者:水星Excel 8,167 次浏览 暂无评论

word表格的行高、列宽既可以单独设置,也可以全部一次性设置为统一的值。

要在vba中单独的调整word表格的行高、列宽,可以使用如下的vba代码:

Sub exceloffice() '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice Dim oDoc As Document Set oDoc = Word.ActiveDocument Dim oT As Table Dim oRow As Row Dim oColumn As Column With oDoc Set oT = .Tables(1) With oT For Each oRow In .Rows oRow.Height = 20 Next For Each oColumn In .Columns oColumn.Width = 110 Next End With End With End Sub

如果要一次就设置所有行高或者列宽为某个值,可以使用如下的vba代码;

Sub exceloffice() '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice Dim oDoc As Document Set oDoc = Word.ActiveDocument Dim oT As Table Dim oRow As Row Dim oColumn As Column With oDoc Set oT = .Tables(1) With oT .Rows.Height = 50 .Columns.Width = 20 End With End With End Sub

如果要使得某个表格的所有列宽平均分窗口宽度,可以使用DistributeWidth方法,代码如下:

Sub exceloffice() '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice Dim oDoc As Document Set oDoc = Word.ActiveDocument Dim oT As Table Dim oRow As Row Dim oColumn As Column With oDoc Set oT = .Tables(1) With oT .Columns.DistributeWidth End With End With End Sub

如果要使得某个表格的列宽刚刚好够容纳他所在的列的单元格内容,而不要有多余的列宽,则可以使用AutoFit方法,代码如下;

Sub exceloffice() '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice Dim oDoc As Document Set oDoc = Word.ActiveDocument Dim oT As Table Dim oRow As Row Dim oColumn As Column With oDoc Set oT = .Tables(1) With oT .Columns(2).AutoFit .Columns.AutoFit End With End With End Sub

AutoFit方法不仅可以一次设置所有的列宽自动自适应单元格内容的宽度,又可以单独设置某个列自适应该列的单元格内容的宽度。

但是如果这个表格已经和整个窗口的宽度一样大了,这个方法就无效了。

 

 

 

其它相关文章推荐:如何用vba删除word表格中的行列?如何用vba给word表格新增插入行列?如何用vba获取word文档中的表格数量?如何用vba批量删除word文档中的表格?如何用vba给word新增、插入表格?如何用vba拆分word表格? 分类 :  VBA , WORD VBA 标签 :  AutoFit , DistributeWidth , 表格的列宽 , 表格的行高        

声明: 本站文章均属原创,未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则视为侵权。如果想要每天都收到本站的技术文章推送,可以微信搜索公众号:水星Excel或者Excel_Office关注。其它事宜请加QQ:1722187970 或微信号:xycgenius 联系。 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3