Foxtable(狐表)用户栏目专家坐堂 → 如何创建WORD文档并且存储到指定的文件夹


  共有9967人关注过本帖树形打印复制链接

主题:如何创建WORD文档并且存储到指定的文件夹

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


加好友 发短信
等级:四尾狐 帖子:810 积分:5250 威望:0 精华:0 注册:2009/2/6 10:38:00
  发帖心情 Post By:2013/9/1 21:17:00 [显示全部帖子]


'操控Word文档,主要是文档合并
'*******************************
'*********以下代码可修改**********
Dim FileName = "复件 干部审计通知.doc"   '定义模版文件名
Dim Ctn As String = "干部审计"  '当前表表名,通用格式Functions.Execute("CurrentTableName")
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 count = App.Documents(FileName).Characters.Count
    Dim rng As MSWord.Range = App.Documents(FileName).Range(Start:=0, End:=count)
    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)

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)

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)

App.Selection.Find.ClearFormatting()
App.Selection.Find.Text = "[委托时间]"
App.Selection.Find.Replacement.ClearFormatting()
App.Selection.Find.Replacement.Text = Format(cr("委托时间"),"yyyy年M月d日")
App.Selection.Find.Execute(Replace:=MSWord.WdReplace.wdReplaceAll)

            '*********以上是代码主体部分,需要修改**********
            '**********************************************
            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()
上述代码通过测试正常,分享给大家

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


加好友 发短信
等级:四尾狐 帖子:810 积分:5250 威望:0 精华:0 注册:2009/2/6 10:38:00
  发帖心情 Post By:2013/9/1 22:01:00 [显示全部帖子]

把图片装进表格,对 表格长高锁定就可以了

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


加好友 发短信
等级:四尾狐 帖子:810 积分:5250 威望:0 精华:0 注册:2009/2/6 10:38:00
  发帖心情 Post By:2013/9/2 14:47:00 [显示全部帖子]

以下是引用cxabc123在2013-8-31 15:28:00的发言:

 

窗口1_Button1_Click

 

Dim app As New MSWord.Application

try

    '得到模版

    Dim fileName = "test2.doc"

    app.Documents.Open(ProjectPath & fileName)

    Dim count = app.Documents(fileName).Characters.Count

    Dim rng As MSWord.Range = app.Documents(fileName).Range(Start:=0, End:=count)

    rng.Select()   '全选

    rng.Copy()   '拷贝

   

    '新建文档

    Dim missing = System.Reflection.Missing.Value

    Dim nDoc = App.Documents.Add(missing, missing, missing, missing)

    nDoc.Activate()

   

    '插入文段

    Dim idx As Integer = 0

    For Each r As Row In Tables("A")

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

        rng.Paste

       

        app.Selection.Find.ClearFormatting()

        app.Selection.Find.Text = "[参赛选手]"

       

        app.Selection.Find.Replacement.ClearFormatting()

        app.Selection.Find.Replacement.Text = r("参赛选手")

       

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

       

        app.Selection.Find.ClearFormatting()

        app.Selection.Find.Text = "[辅导老师]"

       

        app.Selection.Find.Replacement.ClearFormatting()

        app.Selection.Find.Replacement.Text = r("辅导老师")

       

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

        app.Selection.Find.ClearFormatting()

        app.Selection.Find.Text = "[学校]"

       

        app.Selection.Find.Replacement.ClearFormatting()

        app.Selection.Find.Replacement.Text = r("学校")

       

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

       

        idx += 1

    Next

    nDoc.SaveAs("d:\test88.doc")

catch ex As exception

    msgbox(ex.message)

finally

   

    app.Quit

End try

 

Dim proc As New Process

proc.File = "d:\test88.doc"

proc.Start


代码中只有字符引用的代码,图片引用的代码如何请指教。

另外,代码中的idx += 1是什么作用

以上代码有一个问题,就是跨页表格效果不好,主要原因出在rng.Select()   '全选

    rng.Copy()   '拷贝

这两句代码及其相关的代码上,感觉是不能全选,我不知道如何处理,请那位指点

以上代码有一个问题,就是跨页表格效果不好,


 回到顶部
总数 14 上一页 1 2