找回密码
 加入
搜索
查看: 5850|回复: 11

[系统综合] 如何拼合WORD文件中的表格单元?

  [复制链接]
发表于 2013-5-23 22:30:28 | 显示全部楼层 |阅读模式
求教,我要是Autoit操作WPS/MSOffice制作如下图的表格,但是拼合始终不成功,请大神指点一下,谢谢!此外,按照下面的代码,执行完毕后,文本“这是一个测试”总是会在表格的后面,我本意是放在前面的,这个怎么解决呢?还有就是怎么将这个文本设置为居中,字体为4号,黑体?再次谢谢!

$msg = ObjCreate("WPS.Application")
$msg.visible =1
$objdoc=$msg.Documents.Add()
$objdoc.Range.Text='这是一个测试'&@CRLF
$objRange=$objdoc.Range()
$objdoc.Tables.Add($objRange,5,4)
$objTable = $objDoc.Tables(1)
$objRange.Tables(1).Rows(2).Cell(2,3).Merge  #无法拼合
$objRange.Tables(1).Columns(1).Cell(3,4).Merge   #无法拼合
$objTable.Cell(2, 2).Range.Text = "拼合这个"
$objTable.Cell(3, 1).Range.Text = "拼合这个,居中"


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2013-5-23 22:45:25 | 显示全部楼层
你录制宏之后,按照宏转成AU3的就可以了!
 楼主| 发表于 2013-5-24 10:57:43 | 显示全部楼层
你录制宏之后,按照宏转成AU3的就可以了!
penguinl 发表于 2013-5-23 22:45


尝试录制了一下,但不会转换,求教

Sub 宏1()
'
' 宏1 宏
'
'
    Selection.TypeText Text:="这是一个测试"
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveUp Unit:=wdLine, Count:=2
    Selection.MoveRight Unit:=wdCharacter, Count:=6, Extend:=wdExtend
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    Selection.Font.Size = 14
    Selection.Font.Bold = wdToggle
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveUp Unit:=wdLine, Count:=2
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=5, NumColumns:= _
        4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "网格型" Then
            .Style = "网格型"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    Selection.MoveRight Unit:=wdCharacter, Count:=3
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Cells.Merge
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="拼合了"
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:=wdExtend
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Cells.Merge
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="拼合居中"
    Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
    Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Selection.SelectCell
    Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
    Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
    ChangeFileOpenDirectory "C:\Documents and Settings\Administrator\桌面\"
End Sub
 楼主| 发表于 2013-5-24 22:51:33 | 显示全部楼层
大神快点出现吧!都快沉了,郁闷。。。
发表于 2013-5-26 15:56:27 | 显示全部楼层
$word=ObjCreate("word.application")
$word.visible=True
$doc=$word.documents.add
$doc.tables.add($doc.range(0,1),5,4)
For $n=-6 To -1
        $doc.tables(1).borders($n).linestyle=1
Next
With $doc
        .range(.tables(1).cell(2,2).range.start,.tables(1).cell(2,3).range.end).cells.merge
EndWith
$doc.tables(1).cell(2,2).range.text="合并这里"

With $doc
        .range(.tables(1).cell(3,1).range.start,.tables(1).cell(4,1).range.end).select ;cells.merge
EndWith
With $doc.application.selection
        .cells.merge
        .range.text="合并这里,居中"
        .ParagraphFormat.Alignment=1
        .cells.VerticalAlignment=1
EndWith
很奇怪,横向合并可以直接进行,竖向的合并必须选择了才可以
 楼主| 发表于 2013-5-27 10:50:12 | 显示全部楼层
回复 5# kevinch

.range(.tables(1).cell(3,1).range.start,.tables(1).cell(4,1).range.end).select,这个选择方式貌似有点问题,会将3、4行全部选中。而不能只选中(3,1)、(4,1两个单元格)。
发表于 2013-5-27 12:07:12 | 显示全部楼层
不要貌似,测试过再说
 楼主| 发表于 2013-5-28 09:26:02 | 显示全部楼层
不要貌似,测试过再说
kevinch 发表于 2013-5-27 12:07

测试过了吖!看到大神的代码后,第一时间就测试过了,确实会选中并拼合3至4行。
发表于 2013-5-28 12:16:34 | 显示全部楼层
本地测试是正常的,环境:word2010,xp系统
 楼主| 发表于 2013-5-29 16:15:41 | 显示全部楼层
本地测试是正常的,环境:word2010,xp系统
kevinch 发表于 2013-5-28 12:16

环境:Word2007,XP系统不行,谢谢耐心解答!
发表于 2013-5-31 08:49:27 | 显示全部楼层
$word=ObjCreate("word.application")
$word.visible=True
$doc=$word.documents.add
$doc.tables.add($doc.range(0,1),5,4)
For $n=-6 To -1
        $doc.tables(1).borders($n).linestyle=1
Next
With $doc
        .range(.tables(1).cell(2,2).range.start,.tables(1).cell(2,3).range.end).cells.merge
EndWith
$doc.tables(1).cell(2,2).range.text="合并这里"

$doc.tables(1).cell(3,1).range.select

With $doc.application.selection
                .movedown(5,1,1)
        .cells.merge
        .range.text="合并这里,居中"
        .ParagraphFormat.Alignment=1
        .cells.VerticalAlignment=1
EndWith
这个再试一下
发表于 2013-5-31 15:19:38 | 显示全部楼层
本帖最后由 shqf 于 2013-6-3 08:42 编辑
这个再试一下
kevinch 发表于 2013-5-31 08:49
$msg = ObjCreate("word.Application")
$msg.visible = 1
$objdoc = $msg.Documents.Add()
$objSel = $msg.Selection
$objSel.TypeText("这是一个测试")
$objdoc.Tables.Add($objSel.Range, 5, 4)
$objTable = $objdoc.Tables(1)
With $objTable
        If .Style <> "网格型" Then
                .Style = "网格型"
        EndIf
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
EndWith

$Range1 = $objdoc.Range($objTable.Cell(2, 2).Range.Start, $objTable.Cell(2, 3).Range.End)
$Range1.Cells.Merge
$objTable.Cell(2, 2).Range.Text = "拼合这个"

;$Range2 = $objdoc.Range($objTable.Cell(3, 1).Range.Start, $objTable.Cell(4, 1).Range.End)
;$Range2.Cells.Merge
$objTable.cell(3, 1).range.Select
$objdoc.application.selection.movedown(5, 1, 1)
$objdoc.application.selection.cells.merge
$objTable.Cell(3, 1).Range.Text = "合并这里,居中"
同感,竖向合并Cell(3, 1)-Cell(4,2)或Cell(3, 2)-Cell(4,2)都能成功。第1列总不成功(注释掉的2 句),怪了。看了你的代码,学习了,总算把代码完成贴上,供楼主参考。(系统winxpsp3,word2003)
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-12 02:42 , Processed in 0.094861 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表