Word 2007 - 如何将制表符不一致的文本转换为表格?
我有一大堆格式很奇怪的表格,因为当一群人用选项卡制作表格时,人们不能指望所有表格上的选项卡都保持一致。
例如...我有这个表:2列乘11行...但我想要一个不关心列数和行数的宏
space space space **column header** tab tab tab tab **column header**
tab **data** tab tab tab **data** tab tab
tab **data** tab tab **data**
tab **data** tab tab tab tab **data**
tab **data** tab tab **data**
目前,我有这个宏,
Sub ConvertTextToATable()
Selection.ConvertToTable Separator:=wdWhite, AutoFitBehavior:=wdAutoFitFixed
End Sub
我已经尝试过制表符分隔符。 ..但两者都给出相似的结果。每次遇到新的空白字符时,它都会创建一个新列。我想要的是忽略所有空白,并在每次遇到文本时创建一个新单元格,并在每次遇到 \n 或 \r 时创建一个新行。
由于我不能保证每个表都具有与数据相同数量的列标题,因此我认为列数应该由原始伪表文本中的第二行或第三行确定。
I have a whole bunch of tables that were formatted weird, because when a bunch of people make tables out of tabs, one cannot expect the tabbing to be consistent on all of them.
For example... I have this table: 2 cols by 11 rows... but I would like a macro that didn't care about the number of columns and rows
space space space **column header** tab tab tab tab **column header**
tab **data** tab tab tab **data** tab tab
tab **data** tab tab **data**
tab **data** tab tab tab tab **data**
tab **data** tab tab **data**
Currently, I have this macro
Sub ConvertTextToATable()
Selection.ConvertToTable Separator:=wdWhite, AutoFitBehavior:=wdAutoFitFixed
End Sub
I've tried the tab separator... but both give similar results. It creates a new column every time a new white space character is encountered. What I want is ignoring all white space, and creating a new cell every time text is encountered, and creating a new row every time \n or \r is encountered.
And since I can't guarantee that every table with have the same number of column headers as the data, I think the number of columns should be determined by the second or third row in the text of original pseudo table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DerNalia,这需要正则表达式。在 Office VBA 编辑器中添加对 Microsoft VBScript 正则表达式 5.5 的引用:
(来源:david at wbtn.net)
然后,在宏中使用 Regex 对象将选项卡的所有多个实例替换为一个选项卡。然后您可以像平常一样使用 ConvertToTable 命令。
DerNalia, this calls for a Regular Expression. Add a reference to Microsoft VBScript Regular Expressions 5.5 in your Office VBA editor:
(source: david at wbtn.net)
Then, in your Macro use the Regex objects to replace all multiple instances of a tab with just one tab. Then you can use your ConvertToTable command like normal.