以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  wordVBA定位表格自动插入行及数据  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=127739)

--  作者:benwong2015
--  发布时间:2018/11/21 9:07:00
--  wordVBA定位表格自动插入行及数据
红色字体的部分内容是录了VBA来修改的,但却word会出现有进程无法进行自动插入行及数据,请帮忙看看问题出在哪里;

If e.Form.Controls("DateTimePicker3").value Is Nothing Then
    MessageBox.Show("完成日期:" & "不能为空,请检查")
    Return
Else
    Dim dr As Row = Tables("T_Report").Current
    dr("报告状态") = True
    dr("报告员") = User.name
    dr.save()
    Dim tm As String  = ProjectPath & "Attachments\\化妆品流通报告模板.doc" \'指定模板文件
    Dim fl As String = ProjectPath & "Reports\\" & dr("报告编号") & " " & dr("样品名称") & ".Doc" \'指定目标文件
    Dim wrt As New WordReport(Tables("T_Report"),tm,fl) \'定义一个WordReport
    wrt.BuildOne(dr)
    wrt.Quit
    Dim app As New MSWord.Application
    try
        Dim fileName = ProjectPath & "Reports\\" & Tables("T_Report").current("报告编号") & " " & Tables("T_Report").current("样品名称") & ".Doc" \'指定目标文件
        Dim doc As Object = app.Documents.Open(fileName)
        app.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekCurrentPageHeader
        If app.Selection.Find.Execute("CTTREPORT") Then \'查找到指定字符串
            app.Selection.Text =" " & dr("报告编号") & "" \'替换字符串
        End If
        app.Selection.GoTo(What:= app.wdGoToBookmark, Name:="数据结果表")
        With app.ActiveDocument.Bookmarks
            .DefaultSorting = app.wdSortByLocation
            .ShowHidden = False
        End With
        MessageBox.show("1")
        Dim rs As List (of DataRow) = DataTables("T_Item").Select("报告编号 = \'" & dr("报告编号") & "\'")
        Dim cnt As Integer = "0"
        For Each r As DataRow In rs
            app.Selection.InsertRowsBelow(1)
            MessageBox.show("2")
            app.Selection.TypeText(Text:= cnt+1)
            app.Selection.MoveRight(Unit:=app.wdCharacter, Count:=1)
            app.Selection.TypeText(Text:=r("检测项目"))
            app.Selection.MoveRight(Unit:=app.wdCharacter, Count:=1)
            app.Selection.TypeText(Text:=r("检测方法"))
            app.Selection.MoveRight(Unit:=app.wdCharacter, Count:=1)
            app.Selection.TypeText(Text:=r("结果"))
            app.Selection.MoveRight(Unit:=app.wdCharacter, Count:=1)
            app.Selection.TypeText(Text:=r("标准要求"))
            app.Selection.MoveRight(Unit:=app.wdCharacter, Count:=1)
            app.Selection.TypeText(Text:=r("单位"))
            app.Selection.MoveRight(Unit:=app.wdCharacter, Count:=1)
            app.Selection.TypeText(Text:=r("判定"))
            MessageBox.show("3")
        Next
        app.ActiveWindow.ActivePane.View.SeekView =  MSWord.WdSeekView.wdSeekMainDocument
        app.Visible = True
    catch ex As exception
        app.Quit
    finally
    End try

--  作者:有点甜
--  发布时间:2018/11/21 9:27:00
--  

1、实例发上来测试。

 

2、如果是导出数据,参考

 

http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=70762&replyID=486681&skin=1

 

3、或者修改思路,直接用 doc.tables(1) 获取表格,给单元格赋值,如

 

http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=123195&replyID=828848&skin=1