找回密码
 加入
搜索
查看: 5145|回复: 5

[AU3基础] 关于对WORD的操作

[复制链接]
发表于 2012-6-21 23:55:59 | 显示全部楼层 |阅读模式
#include <IE.au3>
#include <word.au3>
$Doc= ObjGet("","word.Application")
$acdoc = $doc.documents(2)
MsgBox(4096, "自动录入系统", $acdoc.range(1,10).text)
$newdoc=$doc.documents(1)

$acdoc.Tables.Add Range:=$acdoc.Range(0,0), NumRows:=6, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed ;新建收文表格

     With $newdoc.Tables(1)

        .Columns(1).PreferredWidth = CentimetersToPoints(2.2) ;第一列宽
        .Rows.Height = CentimetersToPoints(1.5) ;行高
        .Cell(2, 2).Split NumRows:=1, NumColumns:=5 ;拆分第二行单元格
        .Cell(2, 2).PreferredWidth = CentimetersToPoints(5) ;第二行各单元格宽度
        .Cell(2, 3).PreferredWidth = CentimetersToPoints(2.2)
        .Cell(2, 4).PreferredWidth = CentimetersToPoints(3.4)
        .Cell(2, 5).PreferredWidth = CentimetersToPoints(2.2)
        .Cell(2, 6).PreferredWidth = CentimetersToPoints(2.2)
        .Cell(1, 1).Range = "文件题目" ;以下写入项目文本
        .Cell(2, 1).Range = "来文机关"
        .Cell(2, 3).Range = "来文字号"
        .Cell(2, 5).Range = "收文时间"
        .Cell(3, 1).Range = "党委主要领导批示"
        .Cell(4, 1).Range = "政府主要领导意见"
        .Cell(5, 1).Range = "分管领导阅处意见"
        .Cell(6, 1).Range = "承办部门办理意见"
        ;写入来文字号
        r = 0
        Do
            r = r + 1
        Loop While $acdoc.Range(r, r + 1).Text <> "〔" And $acdoc.Range(r, r + 1).Text <> "[" ;计算六角或方括号位置
        lw = $acdoc.Range(0, r).Paragraphs.Count ;来文段数
        s = $acdoc.Range($acdoc.Paragraphs(lw).Range.Start, $acdoc.Paragraphs(lw).Range.End - 1) ;来文字号文本
        .Cell(2, 4).Range = Replace(s, $acdoc.Range(r, r + 1), Chr(13) & $acdoc.Range(r, r + 1)) ;括号前加回车
        ;写入文件标题
        i = InStr(1, $acdoc.Range, "关于")  ;查找关于位置
        bt = $acdoc.Range(0, i).Paragraphs.Count ;标题段数
        Do
            bt = bt + 1
        Loop While Len(Replace($acdoc.Paragraphs(bt).Range, " ", "")) > 2 ;计算清除空格后的字符数
        st = $acdoc.Paragraphs($acdoc.Range(0, i + 2).Paragraphs.Count).Range.Start ;标题起点
        en = $acdoc.Paragraphs(bt - 1).Range.End ;标题终点
        $acdoc.Range(st, en).Copy ;复制标题
        .Cell(1, 2).Range.PasteSpecial DataType:=wdPasteText ;粘贴到表格,因图片会影响I值,故不使用I值
        .Cell(1, 2).Range = Replace(Replace(.Cell(1, 2).Range, " ", ""), Chr(13), "") ;清除标题中间空格和回车
        ;写入来文机关
        If Len(Replace($acdoc.Paragraphs($acdoc.Range(0, i + 2).Paragraphs.Count - 1).Range, " ", "")) > 3 Then ;关于前是否有文字作单位
            Do
                n = n + 1
            Loop While Len(Replace($acdoc.Paragraphs(lw + n).Range, " ", "")) < 2
            $acdoc.Range($acdoc.Paragraphs(lw + n).Range.Start, $acdoc.Paragraphs(lw + n).Range.End).Copy
            .Cell(2, 2).Range.PasteSpecial DataType:=wdPasteText
        ElseIf $acdoc.Content.InlineShapes.Count = 1 Then ;否则同时艺术字为一个时,则使用艺术字内容作单位
            t = $acdoc.Content.InlineShapes(1).TextEffect.Text
            t = Replace(Replace(Replace(Replace(t, "文件", ""), "通知", ""), "(", ""), ")", "") ;去掉通知文件括号等字样
            .Cell(2, 2).Range = t
        Else
             .Cell(2, 2).Range = $acdoc.Range($acdoc.Paragraphs(lw).Range.Start, r)
        End If
        ;写入收文时间
        .Cell(2, 6).Range = Now
        .Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter ;垂直居中
        .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter ;水平居中
     End With
;     $newdoc.PrintOut ;打印收文单
;     $newdoc.Close wdDoNotSaveChanges     ;关闭收文单
上面代码要达到的目的:
本身有一个文档1(正式文件),现新建一个文档2,在文档2中新建一个表格,要求将文档1中的相关内容填写至文档2的表格中,本人已使用WORD的VBA代码写了一个子过程,可以实现我的要求,现在我需要使用AUTOIT来进行编写,但在此过程中就出现了错误,请各位老大指教。

本帖子中包含更多资源

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

×
发表于 2012-6-22 11:05:05 | 显示全部楼层
$acdoc.Tables.Add Range:=$acdoc.Range(0,0), NumRows:=6, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed ;新建收文表格
这种参数表示法不适用au3,要改成类似
$acdoc.Tables.Add($acdoc.Range(0,0),6,2,...)
所有的常量要改成对应的数值,所有的默认值写成default
发表于 2012-6-22 17:30:48 | 显示全部楼层
2楼正解 啊
 楼主| 发表于 2012-6-22 21:16:26 | 显示全部楼层
感谢K兄的答疑,对类似的操作我又学了一招,另外问一下
如果我原有一个文档,文档名及路径不固定,后又新建一文档,请教我如何采取类似
$doc.application.documents.item(1)以序号的方法能够表示出原有的文档,
 楼主| 发表于 2012-6-22 21:16:32 | 显示全部楼层
感谢K兄的答疑,对类似的操作我又学了一招,另外问一下
如果我原有一个文档,文档名及路径不固定,后又新建一文档,请教我如何采取类似
$doc.application.documents.item(1)以序号的方法能够表示出原有的文档,
发表于 2012-6-22 22:33:45 | 显示全部楼层
保险的作法是用for ... in语句来循环打开的文档,判断名称及路径(如果需要判断路径的话),然后执行操作
又或者文档不多的情况下可以直接指定给变量,然后操作就方便了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-20 13:50 , Processed in 0.206022 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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