我想实现这样的一个操作,把一张图片文件插入到word文档中,然后再对其大小进行一次调整,规定其高度,请问能否给出相应的vb代码,小弟在此先谢谢各位高手了!!

解决方案 »

  1.   

    Selection.InlineShapes.AddPicture FileName:= _
            "D:\2003 document\My Pictures\1_151_115.jpg", LinkToFile:=False, _
            SaveWithDocument:=True
        Selection.WholeStory
        Call MsgBox(Selection.InlineShapes.Count)
        Selection.InlineShapes(0).Width = 100
        Selection.InlineShapes(0).Height = 100根据Word的宏录制生成的代码。
      

  2.   

    Private Sub Command1_Click()
        Dim xApp As New Word.Application
        Dim xDoc As Document
        Dim xShape As InlineShape
        
        Set xApp = New Word.Application
        Set xDoc = xApp.Documents.Add
        'Set xDoc = xApp.ActiveDocument
        Set xShape = xDoc.InlineShapes.AddPicture("D:\2003 document\My Pictures\1_151_115.jpg")
        xShape.Width = 200
        xShape.Height = 200
        xDoc.SaveAs "c:\dfgh.doc"
        Set xShape = Nothing
        xDoc.Close
        xApp.Quit
        
        Set xDoc = Nothing
        Set xApp = Nothing
    End Sub运行上面的代码首先需要引用Microsoft Word Object Library
      

  3.   

    Selection.WholeStory---这一句代表的是什么意思啊?
    Selection.InlineShapes(0).Width = 100--这一句中的InlineShapes(0)又是什么意思,如果我有很多的图片呢?是不是还是用InlineShapes(0)?
      

  4.   

    使用了Selection.WholeStory之后插入的图片就不在了呀??
      

  5.   

    你不要用第一个代码,这是从Word宏记录中导入的,有些问题,用第二个,这个比较完善。
      

  6.   

    Private Sub Command1_Click()
        Dim xApp As New Word.Application
        Dim xDoc As Document
        Dim xShape As InlineShape
        
        Set xApp = New Word.Application
        
        '添加文档并返回文档对象
        Set xDoc = xApp.Documents.Add
        
        '插入一个图形Shape并返回其对象引用
        Set xShape = xDoc.InlineShapes.AddPicture("D:\2003 document\My Pictures\1_151_115.jpg")
        
        '设置Shape的大小
        xShape.Width = 200
        xShape.Height = 200
        
        Set xShape = Nothing
        
        '保存
        xDoc.SaveAs "c:\dfgh.doc"
        
        xDoc.Close
        xApp.Quit
        
        Set xDoc = Nothing
        Set xApp = Nothing
    End Sub
    这是上面代码的注释,可能对你的理解有一些帮助。如果需要多次插入图片,重复调用
        '插入一个图形Shape并返回其对象引用
        Set xShape = xDoc.InlineShapes.AddPicture("D:\2003 document\My Pictures\1_151_115.jpg")
        
        '设置Shape的大小
        xShape.Width = 200
        xShape.Height = 200
        
        Set xShape = Nothing
    就可以了。
      

  7.   

    If Not rs.EOF Then
        For i = 1 To rs.RecordCount
        filepicture = AppPath & "\picture\" & Trim(rs("lx")) & "\" & "20" & Mid(Trim(rs("ajbh")), 7, 2) & "\" & Trim(rs("wjm")) & Trim(rs("kzm"))
        oWordApp.Selection.InlineShapes.AddPicture filepicture, False, True
        oWordApp.Selection.WholeStory
        oWordApp.Selection.InlineShapes(0).Width = 200
        oWordApp.Selection.InlineShapes(0).Height = 200                
        rs.MoveNext
        Next
        SaveFileName = AppPath & "\word\" & lx & "_word\" & ajbh & ".doc"
        oWordApp.ActiveDocument.SaveAs SaveFileName
    End If我的这一段代码执行后为什么会插入进去的图片会不见了呢??我的类型库引用都是对的呀!!
      

  8.   

    我的msn:
    [email protected]
      

  9.   

    如果是多个图片插入的话不能用oWordApp.Selection.WholeStory,因为这是对文档进行全选,所以再次插入的话就会将原来全选的内容替换掉,用我上面提供的代码。
      

  10.   

    插入了图片后不是有一个默认的width和height吗?你用一个固定的值去除这两者值就实现了等比例缩放啦。
      

  11.   

    在插入两个图片的代码中间插入:
    Selection.TypeParagraph
    就可以实现在两个图片中间换行。