wordvba表格居中.docx
- 文档编号:7996498
- 上传时间:2023-01-27
- 格式:DOCX
- 页数:6
- 大小:16.49KB
wordvba表格居中.docx
《wordvba表格居中.docx》由会员分享,可在线阅读,更多相关《wordvba表格居中.docx(6页珍藏版)》请在冰豆网上搜索。
wordvba表格居中
竭诚为您提供优质文档/双击可除
word,vba,表格居中
篇一:
巧用Vba自动处理word表格
巧用Vba自动处理word表格发布:
20xx-8-0817:
59|作者:
/retype/zoom/aaabe40416fc700abb68fc9dpn=1图1@@
setodoc=activedocument
setotable=odoc.tables.add(Range:
=odoc.Range(start:
=0,end:
=0),numRows:
=3,numcolumns:
=4)
icount=1
Foreachocellinotable.Range.cells
ocell.Range.insertafter"第"图2@@
(2)如果需要在表格的第二列插入星期值,可在上例的Foreach...next结构中插入以下几行:
ificountmod4=2andicount>4then
ocell.Range.insertaftercweekname(weekday(date+(icount-1)/4))
endif
其中,weekday(date)返回一数值(1~7),分别表示"星期日"~"星期六",cweekname数组需要事先定义为:
dimcweekname(7)
cweekname
(1)="星期日"
cweekname
(2)="星期一"
......
cweekname(7)="星期六"
4.根据单元格的内容设置不同的格式
以上例中表格为例,如果需要将所有"星期六"和"星期日"所在行格式改为蓝色背景,只要在上例程序之后追加以下几行即可(表格格式改为wdtableFormatcolorful2,行数改为12行)。
程序中再次使用Foreach...next结构遍历表格中的每一行(Rows),如果检测到某一行满足条件("星期六"或"星期日"),则选择一行(selection.selectRow),将其属性改为需要的格式(本例中为蓝色背景)。
icount=1
ForeachRowsinotable.Range.Rows
if(weekday(date+(icount-1))=7orweekday(date+(icount-1))=1)andicount>1
then
selection.selectRow
withselection.cells
with.shading
.texture=wdtexturenone
.Foregroundpatterncolorindex=wdauto
.backgroundpatterncolorindex=wdblue
endwith
endwith
endif
icount=icount+1
selection.movedownunit:
=wdline,count:
=1
nextRows
@@0869602.jpg;图3@@
以上几例简要介绍了使用Vba自动处理word表格的例子(有关Visualbasic事件、方法、对象、属性的详细使用方法请参阅microsoftwordVisualbasic帮助)。
客户可将编制的Visualbasic代码作为word宏指定到工具栏或快捷方式,方便以后的使用。
用Vba操作word表格
20xx-12-2215:
20
word的表格功能是非常重要的一个功能,也是用户经常使用的一项功能,在word20xx中,增加了不少新的功能。
如果利用Vba自动处理表格将使用户的效率有极大的提高。
1.向表格单元格插入文字
下面的代码向活动文档的第一个表格的第一个单元格插入文字。
cell方法返回单个的cell对象。
Range属性返回一个Range对象。
delete方法用来删除现有的文字,而insertafter方法用来插入"cell1,1"文字。
ifactivedocument.tables.count>=1then
withactivedocument.tables
(1).cell(Row:
=1,column:
=1).Range
.delete
.insertaftertext:
="cell1,1"
endwith
endif
2在表格中插入文字
下面的代码在文档的开头插入一张3行4列的表格。
Foreach...next结构用来循环遍历表格中的每个单元格。
在Foreach...next结构中,insertafter方法用来向表格单元格(cell1、cell2等等)添加文字。
setodoc=activedocument
setotable=odoc.tables.add(Range:
=odoc.Range(start:
=0,end:
=0),numRows:
=3,numcolumns:
=4)
icount=1
Foreachocellinotable.Range.cells
ocell.Range.insertafter"cell"非常重要,目的是去掉换行符
否则内容后面会有个小圆点
msgboxmyRange.text
nextacell
4将文本转换为表格
下面的代码在活动文档的开头插入以制表符分隔的文本,然后将这些文本转换为一张表格。
setoRange1=activedocument.Range(start:
=0,end:
=0)
oRange1.insertbefore"one"防止未选择段落时执行程序的误操作
withsel.paragraphFormat
.space15
.disablelineheightgrid=False
endwith
Fori=1tosel.paragraphs.count
withsel.paragraphs(i)
str=.Range.text
char1=left(str,1)
ifchar1=""then
dimjasinteger
j=0
while(char1=""andlen(str)>0)
j=j+1
str=mid(str,2)
char1=left(str,1)
wend
activedocument.Range(.Range.start,.Range.characters(j).end).deleteendif
if.style=activedocument.styles("标题1")or.style=_
activedocument.styles("标题2")or.style=activedocument.styles("标题3")then
.alignment=wdalignparagraphcenter
.characterunitFirstlineindent=0
else
.characterunitFirstlineindent=2
endif
endwith
next
sel.select
endifendsub
篇三:
巧用Vba自动处理word表格
microsoftword97是大家熟悉的文字处理软件,强大的功能为我们的工作提供了很大的帮助。
而Visualbasicforapplications(Vba)的应用更为word97增添了不少特色,合理而恰当地使用Vba可为用户提供极大的方便。
下面介绍几则使用Vba编程自动处理word表格的例子。
1.创建表格,插入文字
本例的功能是在文档开头插入一张3行4列的表格。
可用Foreach...next结构来循环遍历表格中的每个单元格。
在Foreach...next结构中,insertafter方法用来向表格单元格添加文字("第1单元格"、"第2单元格"等等),otable.autoFormat属性用于指定表格套用格式。
运行结果如图1:
@@0869600.jpg;图1@@
setodoc=activedocument
setotable=odoc.tables.add(Range:
=odoc.Range(start:
=0,end:
=0),numRows:
=3,numcolumns:
=4)
icount=1
Foreachocellinotable.Range.cells
ocell.Range.insertafter"第"图2@@
(2)如果需要在表格的第二列插入星期值,可在上例的Foreach...next结构中插入以下几行:
ificountmod4=2andicount>4then
ocell.Range.insertaftercweekname(weekday(date+(icount-1)/4))
endif
其中,weekday(date)返回一数值(1~7),分别表示"星期日"~"星期六",cweekname数组需要事先定义为:
dimcweekname(7)
cweekname
(1)="星期日"
cweekname
(2)="星期一"
......
cweekname(7)="星期六"
4.根据单元格的内容设置不同的格式
以上例中表格为例,如果需要将所有"星期六"和"星期日"所在行格式改为蓝色背景,只要在上例程序之后追加以下几行即可(表格格式改为wdtableFormatcolorful2,行数改为12行)。
程序中再次使用Foreach...next结构遍历表格中的每一行(Rows),如果检测到某一行满足条件("星期六"或"星期日"),则选择一行(selection.selectRow),将其属性改为需要的格式(本例中为蓝色背景)。
icount=1
ForeachRowsinotable.Range.Rows
if(weekday(date+(icount-1))=7orweekday(date+(icount-1))=1)andicount>1
then
selection.selectRow
withselection.cells
with.shading
.texture=wdtexturenone
.Foregroundpatterncolorindex=wdauto
.backgroundpatterncolorindex=wdblue
endwith
endwith
endif
icount=icount+1
selection.movedownunit:
=wdline,count:
=1
nextRows
@@0869602.jpg;图3@@
以上几例简要介绍了使用Vba自动处理word表格的例子(有关Visualbasic事件、方法、对象、属性的详细使用方法请参阅microsoftwordVisualbasic帮助)。
客户可将编制的Visualbasic代码作为word宏指定到工具栏或快捷方式,方便以后的使用。
用Vba操作word表格
20xx-12-2215:
20
word的表格功能是非常重要的一个功能,也是用户经常使用的一项功能,在word20xx中,增加了不少新的功能。
如果利用Vba自动处理表格将使用户的效率有极大的提高。
1.向表格单元格插入文字
下面的代码向活动文档的第一个表格的第一个单元格插入文字。
cell方法返回单个的cell对象。
Range属性返回一个Range对象。
delete方法用来删除现有的文字,而insertafter方法用来插入"cell1,1"文字。
ifactivedocument.tables.count>=1then
withactivedocument.tables
(1).cell(Row:
=1,column:
=1).Range
.delete
.insertaftertext:
="cell1,1"
endwith
endif
2在表格中插入文字
下面的代码在文档的开头插入一张3行4列的表格。
Foreach...next结构用来循环遍历表格中的每个单元格。
在Foreach...next结构中,insertafter方法用来向表格单元格(cell1、cell2等等)添加文字。
setodoc=activedocument
setotable=odoc.tables.add(Range:
=odoc.Range(start:
=0,end:
=0),numRows:
=3,numcolumns:
=4)
icount=1
Foreachocellinotable.Range.cells
ocell.Range.insertafter"cell"非常重要,目的是去掉换行符
否则内容后面会有个小圆点
msgboxmyRange.text
nextacell
4将文本转换为表格
下面的代码在活动文档的开头插入以制表符分隔的文本,然后将这些文本转换为一张表格。
setoRange1=activedocument.Range(start:
=0,end:
=0)
oRange1.insertbefore"one"&vbtab&"two"&vbtab&"three"&vbcr
setotable1=oRange1.converttotable(separator:
=chr(9),numRows:
=1,numcolumns:
=3)
5返回每个表格单元格的内容
下面的代码定义一个数组,使该数组的元素个数等于文档第一个表格中的单元格数(假定optionbase1)Foreach...next结构用来返回每个表格单元格的内容,并将文字指定给相应的数组元素。
ifactivedocument.tables.count>=1then
setotable=activedocument.tables
(1)
inumcells=otable.Range.cells.count
Redimacells(inumcells)
i=1
Foreachocellinotable.Range.cells
setmyRange=ocell.Range
myRange.moveendunit:
=wdcharacter,count:
=-1
acells(i)=myRange.text
i=i+1
nextocell
endif
6将活动文档中的所有表格复制到一篇新文档
下面的代码将当前文档中的表格复制到新文档中。
ifactivedocument.tables.count>=1then
setodoc1=activedocument
setmyRange=documents.add.Range(start:
=0,end:
=0)
Foreachotableinodoc1.tables
otable.Range.copy
withmyRange
.paste
.collapsedirection:
=wdcollapseend
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- wordvba 表格 居中
![提示](https://static.bdocx.com/images/bang_tan.gif)