Foxtable(狐表)用户栏目专家坐堂 → wordVBA定位表格自动插入行及数据


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

主题:wordVBA定位表格自动插入行及数据

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


加好友 发短信
等级:幼狐 帖子:113 积分:1467 威望:0 精华:0 注册:2018/7/4 8:21:00
wordVBA定位表格自动插入行及数据  发帖心情 Post By:2018/11/21 9:07:00 [只看该作者]

红色字体的部分内容是录了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

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


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By: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

 

 

 


 回到顶部