Foxtable(狐表)用户栏目专家坐堂 → 求帮助写段从Word读取数据的代码。


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

主题:求帮助写段从Word读取数据的代码。

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


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/7/9 20:18:00 [显示全部帖子]

 mark word读取图片

 

Dim app As New MSWord.Application
try
    Dim fileName = "C:\Documents and Settings\Administrator\My Documents\下载\从word取数据\从WORD取数据\黄某某.doc"
    Dim doc = app.Documents.Open(fileName)
    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("d:\test.jpg")
        End If
    Next
catch ex As exception
    msgbox(ex.message)
finally
    app.Quit
End try


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


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/7/9 20:22:00 [显示全部帖子]

 读取某个单元格的值,参考

 

Dim app As New MSWord.Application
try
    Dim fileName = "C:\Documents and Settings\Administrator\My Documents\下载\从word取数据\从WORD取数据\黄某某.doc"
    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)
    msgbox(text)
catch ex As exception
    msgbox(ex.message)
finally
    app.Quit
End try


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


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/7/9 20:31:00 [显示全部帖子]

 按钮代码

 

Dim dlg As new OpenFileDialog
dlg.MultiSelect = True
If dlg.ShowDialog = DialogResult.OK Then
    Dim app As New MSWord.Application
    try
        If FileSys.DirectoryExists(ProjectPath & "Attachments") = False Then
            FileSys.CreateDirectory(ProjectPath & "Attachments/")
        End If
        For Each filename As String In dlg.FileNames
           
            Dim doc = app.Documents.Open(fileName)
            Dim nr As Row = Tables("个人信息表").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
        Next
    catch ex As exception
        msgbox(ex.message)
    finally
        app.Quit
    End try
End If


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


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/7/21 22:23:00 [显示全部帖子]

    Dim t = doc.Tables(1)
    Dim text = t.Cell(3, 2).Range.Text.ToString()
    text = text.Substring(0, text.Length - 2)
    text = text.replace(chr(13), vbcrlf)
    msgbox(text)

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


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2016/8/8 20:11:00 [显示全部帖子]

问题一:

 

1、

 

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

 

2、如果1无法修复,就重新安装一下wps。

 

 

问题二:上传实例


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


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2016/8/8 23:01:00 [显示全部帖子]

分开处理

 

Dim app As New MSWord.Application
try
    Dim fileName = "d:\test.doc"
    Dim doc = app.Documents.Open(fileName)
   
    For Each shape As object In doc.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("d:\test.jpg")
            msgbox(1)
        End If
    Next
    For Each shape As object In doc.Shapes

msgbox(shape.Type)
        If shape.Type = 13   
            Dim img As Byte() = shape.Range.EnhMetaFileBits
            Dim bmp As new Bitmap(new IO.MemoryStream(img))
            'bmp.Save("d:\test.jpg")
            msgbox(2)
        End If
    Next
catch ex As exception
    msgbox(ex.message)
finally
    app.Quit
End try

 

[此贴子已经被作者于2016/8/8 23:01:38编辑过]

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


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2016/8/9 10:26:00 [显示全部帖子]

改一下

 

Dim app As New MSWord.Application
try
    Dim fileName = "d:\test.doc"
    Dim doc = app.Documents.Open(fileName)
   
    For Each shape As object In doc.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("d:\test456.jpg")
        End If
    Next
    For Each shape As object In doc.Shapes
       
        msgbox(shape.Type)
        If shape.Type = 13
            shape = shape.ConvertToInlineShape
            Dim img As Byte() = shape.Range.EnhMetaFileBits
            Dim bmp As new Bitmap(new IO.MemoryStream(img))
            bmp.Save("d:\test123.jpg")
        End If
    Next
    doc.saved = true
catch ex As exception
    msgbox(ex.message)
finally
    app.Quit
End try


 


 回到顶部