Foxtable(狐表)用户栏目专家坐堂 → WORD导入表代码修改


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

主题:WORD导入表代码修改

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


加好友 发短信
等级:小狐 帖子:394 积分:6003 威望:0 精华:0 注册:2015/2/17 10:18:00
WORD导入表代码修改  发帖心情 Post By:2017/7/6 22:44:00 [只看该作者]

从D:\LZXX1目录讲WORD 表格信息的姓名、照片自动批量导入表A 姓名、照片列。
用如下代码成功批量导入,但刚执行前会提示    “集合所要求的成员不存在“,后面不影响导入,请老师修改。谢谢!
For Each AAA As String In filesys .GetFiles("d:\lzxx1")
Dim app As New MSWord.Application
try
    Dim fileName = AAA
    Dim doc = app.Documents.Open(fileName)
    Dim t = doc.Tables(1)
    Dim text = t.Cell(1, 2).Range.Text.ToString()
    text = text.Substring(0, text.Length - 2)
Dim nr As Row = Tables("表A").AddNew
            'Dim t = doc.Tables(1)
           ' Dim text = t.Cell(1, 2).Range.Text.ToString()
           ' text = text.Substring(0, text.Length - 2)
            nr("姓名") = text
app.ActiveWindow.Selection.WholeStory
            For Each shape As object In app.ActiveWindow.Selection.InlineShapes
                If shape.Type = MSWord.WdInlineShapeType.wdInlineShapePicture
                    Dim img As Byte() = shape.Range.EnhMetaFileBits
                    Dim bmp As new Bitmap(new IO.MemoryStream(img))
                    bmp.Save(ProjectPath & "Attachments/" & nr("姓名") & ".jpg")
                End If
            Next
            nr("照片") = nr("姓名") & ".jpg"
            Doc.Close
catch ex As exception
    msgbox(ex.message)
finally
    app.Quit
End try
Next















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


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/7/6 23:38:00 [只看该作者]

如果你的word文件没有表格,那么使用doc.tables(1)获取就会报错。

 

你可以判断表格个数 if doc.tables.count >= 1 then

 

For Each AAA As String In filesys .GetFiles("d:\lzxx1")
    Dim app As New MSWord.Application
    try
        Dim fileName = AAA
        Dim doc = app.Documents.Open(fileName)
        Dim t = doc.Tables(1)
        Dim text = t.Cell(1, 2).Range.Text.ToString()
        text = text.Substring(0, text.Length - 2)
        Dim nr As Row = Tables("表A").AddNew
        'Dim t = doc.Tables(1)
        ' Dim text = t.Cell(1, 2).Range.Text.ToString()
        ' text = text.Substring(0, text.Length - 2)
        nr("姓名") = text
        app.ActiveWindow.Selection.WholeStory
        For Each shape As object In app.ActiveWindow.Selection.InlineShapes
            If shape.Type = MSWord.WdInlineShapeType.wdInlineShapePicture
                Dim img As Byte() = shape.Range.EnhMetaFileBits
                Dim bmp As new Bitmap(new IO.MemoryStream(img))
                bmp.Save(ProjectPath & "Attachments/" & nr("姓名") & ".jpg")
            End If
        Next
        nr("照片") = nr("姓名") & ".jpg"
        Doc.Close
    catch ex As exception
        'msgbox(ex.message)
    finally
        app.Quit
    End try
Next


 回到顶部