Foxtable(狐表)用户栏目专家坐堂 → Foxtable操控Word的代码(测试成功,分享大家)


  共有5516人关注过本帖平板打印复制链接

主题:Foxtable操控Word的代码(测试成功,分享大家)

帅哥哟,离线,有人找我吗?
cxabc123
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:810 积分:5250 威望:0 精华:0 注册:2009/2/6 10:38:00
Foxtable操控Word的代码(测试成功,分享大家)  发帖心情 Post By:2013/9/2 22:18:00 [只看该作者]

 

'操控Word文档,主要是文档合并

'*******************************

'*********以下代码可修改**********

Dim FileName = "任免审批表.doc"   '定义模版文件名

Dim Ctn As String = "EmptyTable"  '当前表表名

Dim Tb As Table = Tables(Ctn)   '定义当前表.

 

'*********以上代码可修改**********

'*******************************

 

If FileSys.DirectoryExists(ProjectPath & "Reports\") = False Then   '如果Reports文件夹不存在

    FileSys.CreateDirectory(ProjectPath & "Reports\")    '创建Reports文件夹

End If

Dim App As New MSWord.Application    '定义MSWord

Try

    '获得模版

    Dim nDoc = App.Documents.Open(ProjectPath & "Attachments\" & FileName)

    Dim rng As MSWord.Range = App.Documents(FileName).Range

    rng.Select()   '全选

    rng.Copy()   '拷贝

    nDoc.Activate()

   

    '插入文段

    Dim idx As Integer = 0

    If Tb.Rows.Count > 0 Then

        For i As Integer = Tb.TopPosition To Tb.BottomPosition

            Dim cr As Row = Tb.Rows(i)

            If idx >= 1 Then

                rng = nDoc.Range(start:=0, End:=0)  '从前面粘贴

                rng.Paste

            End If

           

            '***********************************************

            '*********以下是代码主体部分,需要修改**********

            

           

            App.Selection.Find.ClearFormatting()

            App.Selection.Find.Text = "《姓名》"

            App.Selection.Find.Replacement.ClearFormatting()

            App.Selection.Find.Replacement.Text = cr("姓名")

            App.Selection.Find.Execute(Replace:=MSWord.WdReplace.wdReplaceAll)

           

           

            rng.Find.ClearFormatting()

            If rng.Find.Execute("《照片》") Then

                rng.Select()

                If FileSys.FileExists(ProjectPath & "Attachments\" & cr("图片")) Then

                    App.Selection.InlineShapes.AddPicture(ProjectPath & "Attachments\" & cr("图片"))

                End If

                rng = App.Documents(FileName).Range

                'rng.SetRange(start:=0, End:=count)

                rng.Select()

            End If

           

            '*********以上是代码主体部分,需要修改**********

            '**********************************************

            idx += 1

        Next

    End If

   

    nDoc.SaveAs(ProjectPath & "Reports\" & FileName)

Catch ex As exception

    msgbox(ex.message)

Finally

    App.Quit

End Try

 

Dim Proc As New Process

Proc.File = ProjectPath & "Reports\" & FileName

Proc.Start

 以上代码经过测试基本 成功。分享大家。

[此贴子已经被作者于2013-9-2 22:22:20编辑过]

 回到顶部
总数 19 1 2 下一页