vba 自动调整表格内容但具有最大表格宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20891279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autofit table contents but with max table width
提问by JTFRage
I'm trying to autofit table contents in word via vba, however when using autofit, the columns shrink to the right size, but the table size also shrinks in width. How do I keep the table width at its max?
我正在尝试通过 vba 在 word 中自动调整表格内容,但是在使用自动调整时,列会缩小到正确的大小,但表格的宽度也会缩小。如何将表格宽度保持在最大值?
Code:
代码:
With tableEmployees
.AllowAutoFit = True
.AutoFitBehavior wdAutoFitWindow
.Columns(1).AutoFit
.Columns(2).AutoFit
.Columns(3).AutoFit
.Columns(4).AutoFit
End With
Table Structure:
表结构:
id | name | phone | email |
1 | john doe | 555-5555 | john.doe@someadd
ress.com
When I autofit the columns so that the e-mail address appears on one line, the table width shrinks in size. I want the table to take up the empty space on the page so it fills up to the right. The table I have above this one is at full width and I'd like this one to be at full width beneath it but all cell contents visible on one line since there is space for them.
当我自动调整列以便电子邮件地址出现在一行时,表格宽度会缩小。我希望表格占据页面上的空白区域,使其向右填充。我在这个上面的表格是全宽的,我希望这个表格在它下面是全宽的,但所有单元格内容都在一行上可见,因为它们有空间。
采纳答案by JTFRage
Okay I was able to solve my own problem with the following modifications:
好的,我能够通过以下修改解决我自己的问题:
With tableEmployees
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Columns(1).PreferredWidth = 15
.Columns(2).PreferredWidth = 25
.Columns(3).PreferredWidth = 20
.Columns(4).PreferredWidth = 40
End With
Works nicely!
很好用!
回答by seanv
Something like this has worked for me:
这样的事情对我有用:
With tableEmployees
.AllowAutoFit = True
.AutoFitBehavior wdAutoFitContent
.Columns.AutoFit
.AutoFitBehavior wdAutoFitWindow
.Columns.AutoFit
End With
Autofit to content sizes the columns reasonably with respect to each other.
自动适应内容大小的列相对于彼此合理。
Window is the width between margins and column borders, so autofit to window will resize the table to the page width inside the margins, and retains the proportions set by autofit to content.
窗口是边距和列边框之间的宽度,因此自动适应窗口会将表格的大小调整为边距内的页面宽度,并保留自动适应内容设置的比例。