注册 登录
编程论坛 VB6论坛

求助,怎么将指定文件夹的图片插入这个WORD2007内

huwenyi7758 发布于 2015-07-23 09:50, 2107 次点击
Private Sub Command5_Click()
Dim L1 As Double, L2 As Double, L3 As Double
L1 = Text11.Text
L2 = Text12.Text
L3 = Text13.Text
Label54.Caption = L1 + L2 + L3
Label55.Caption = L1 * L2 * L3
Label58.Caption = Text9.Text
Label59.Caption = Text10.Text

 Dim wordObj
    Set wordObj = CreateObject("Word.Application")
    With wordObj.Documents.Open("E:\编程\计算书模板\1+1.docx")
      CommonDialog1.Filter = "Word文档(*.docx)|*.docx" '存储文件
        CommonDialog1.ShowSave
        If CommonDialog1.FileName = "" Then
        K1 = 3
        Else
        With .Content
       .Find.MatchCase = True
        
        
            .Find.Execute "{cd1}", , , , , , , , , Text11, 2
            .Find.Execute "{cd2}", , , , , , , , , Text12, 2
            .Find.Execute "{cd3}", , , , , , , , , Text13, 2
            .Find.Execute "{cd4}", , , , , , , , , Label54, 2
            .Find.Execute "{cd5}", , , , , , , , , Label55, 2
            .Find.Execute "{cd6}", , , , , , , , , Label58, 2
            .Find.Execute "{cd7}", , , , , , , , , Label59, 2
         
            
            

        End With
 .SaveAs CommonDialog1.FileName
       End If
    End With
    wordObj.quit
End Sub

这个程序已经可以实现我单击command5弹出一个保存的界面,然后保存即可。
打开保存的word {cd1-7}已经用算好的数据替换好了 我想在该word居中位置先插入一张指定图片 不知道该加什么代码
图片位置 C:\Users\Administrator\Desktop\捕捉和数据库\捕获.GIF
求大神指导
或者有什么其他办法都可

17 回复
#2
lianyicq2015-07-23 10:26
回复 楼主 huwenyi7758
给你一个简单的插入图片的例子,自己多试。
程序代码:
Option Explicit

Private Sub Command1_Click()
  Dim wrdApp As Word.Application
  Dim wrdDoc As Word.Document
  Set wrdApp = CreateObject("Word.Application")
  Dim objWdRange As Word.Range
  Dim GraphImage As String
  GraphImage = "c:\source.bmp"
  wrdApp.Visible = True
  Set wrdDoc = wrdApp.Documents.Open("C:\1.docx")
  With wrdDoc
    Dim wrdPic As Word.InlineShape
    Set wrdPic = .Range.InlineShapes.AddPicture(FileName:=GraphImage, LinkToFile:=False, SaveWithDocument:=True)
    wrdPic.ScaleHeight = 50
    wrdPic.ScaleWidth = 50
    .SaveAs "c:\testp.doc"
  End With
  wrdDoc.Close
  Set wrdDoc = Nothing
  wrdApp.Quit
  Set wrdApp = Nothing
End Sub

#3
huwenyi77582015-07-23 10:36
回复 2楼 lianyicq
我百度很多次 好像很多程序一运行,这个"wrdApp As Word.Application"就显示 编辑错误:用户定义类型未定义 - -
#4
风吹过b2015-07-23 11:57
我百度很多次 好像很多程序一运行,这个"wrdApp As Word.Application"就显示 编辑错误:用户定义类型未定义 - -

需要在 工程里 引用 WORD 库,

我没装WORD了,没办法给你抓图。
#5
huwenyi77582015-07-23 12:06
回复 4楼 风吹过b
那个引用什么word 11.0 object library嘛。。百度也这么说。但是点了勾确定了一样没用
#6
lianyicq2015-07-23 12:08
回复 3楼 huwenyi7758
看你描述问题的时候,VB6已经能够操作WORD文件了,说明已经引用了.
#7
huwenyi77582015-07-23 15:17
回复 6楼 lianyicq
我自己的程序就是原word在E:\编程\计算书模板\1+1伸缩臂.docx里面,然后输出的word用程序另存为了一个新创的空word CommonDialog1.Filter = "Word文档(*.docx)|*.docx" '存储文件。。我把你程序dim全删了之后图片是保存进了原word那里,而不是新创建的那个空word。。这样想输出的数值和图片就分开了。。。
#8
lianyicq2015-07-23 16:07
回复 7楼 huwenyi7758
你不能把Set wrdDoc = wrdApp.Documents.Open("C:\1.docx")
在的"c:\1.docx"改为你新建的文档?
#9
huwenyi77582015-07-23 16:36
回复 8楼 lianyicq
改了一下午了,你这个我也试过,但是改了之后图片都输出不了了,不改还能输出到之前那个原word,可能和我的程序可能冲突了,反正合不到一起- -毕竟是新手,我也不知道怎样把你的程序和我的合一起才能正常运行成功——!。
#10
lianyicq2015-07-23 16:41
回复 9楼 huwenyi7758
那就先把思路整理清楚,逐个功能完善
#11
huwenyi77582015-07-23 16:51
回复 10楼 lianyicq
哎,无法, 才学20天,这些难一点的基本要找程序乱改,又看不懂,只能改好运行满足要求就好。。其实就是想把之前原word{cd1-7}代替成了数值再加一项加图片,然后一起保存  CommonDialog1.Filter = "Word文档(*.docx)|*.docx" '存储文件 弹出一个保存的界面存好,这就是所要输出的word而不改动原word 但是加来加去都不知道怎么把2个程序整合一起。- -!
#12
lianyicq2015-07-24 16:18
回复 11楼 huwenyi7758
没法调试,只加了图片相关代码,你试试,有问题再改。不知道图片的插入位置
程序代码:
Private Sub Command5_Click()
  Dim L1 As Double, L2 As Double, L3 As Double
  Dim wrdDoc As Word.Document 'Modify
  Dim wrdPic As Word.InlineShape 'Modify
  L1 = Text11.Text
  L2 = Text12.Text
  L3 = Text13.Text
  Label54.Caption = L1 + L2 + L3
  Label55.Caption = L1 * L2 * L3
  Label58.Caption = Text9.Text
  Label59.Caption = Text10.Text
  Dim wordObj
  Set wordObj = CreateObject("Word.Application")
  Set wrdDoc = wordObj.Documents.Open("E:\编程\计算书模板\1+1.docx") 'Modify
  With wrdDoc 'Modify
  'With wordObj.Documents.Open("E:\编程\计算书模板\1+1.docx")
    CommonDialog1.Filter = "Word文档(*.docx)|*.docx" '存储文件
    CommonDialog1.ShowSave
    If CommonDialog1.FileName = "" Then
      K1 = 3
    Else
      With .Content
        .Find.MatchCase = True
        .Find.Execute "{cd1}", , , , , , , , , Text11, 2
        .Find.Execute "{cd2}", , , , , , , , , Text12, 2
        .Find.Execute "{cd3}", , , , , , , , , Text13, 2
        .Find.Execute "{cd4}", , , , , , , , , Label54, 2
        .Find.Execute "{cd5}", , , , , , , , , Label55, 2
        .Find.Execute "{cd6}", , , , , , , , , Label58, 2
        .Find.Execute "{cd7}", , , , , , , , , Label59, 2
      End With
      Set wrdPic = wrdDoc.Range.InlineShapes.AddPicture(FileName:="图片文件路径及名称", LinkToFile:=False, SaveWithDocument:=True) 'Modify
      wrdPic.ScaleHeight = 50 'Modify
      wrdPic.ScaleWidth = 50 'Modify
      .SaveAs CommonDialog1.FileName
    End If
  End With
  wordObj.Quit
End Sub

#13
huwenyi77582015-07-24 18:57
回复 12楼 lianyicq
Dim wrdDoc As Word.Document 'Modify
   Dim wrdPic As Word.InlineShape 'Modify
这个我还是把Word.Document ,Word.InlineShape 删了就成功运行了,不然的话还是未定义什么的,但是成功运行了就好,就是不知道那些嵌入式什么的调照片的位置的代码不知道会搜的到不-。太谢谢你了。
#14
huwenyi77582015-07-25 08:54
回复 12楼 lianyicq
大神如果有设置图片位置的相关代码可以回复一下嘛,网上好多代码都乱,也完全不知道加在哪个位置,,。谢谢了
#15
huwenyi77582015-07-25 10:28
回复 12楼 lianyicq
已经知道怎么确定位置了,还有多张图片插入。但是就是不知道怎么设置插入在该word的第几页。
#16
lianyicq2015-07-26 07:46
回复 15楼 huwenyi7758
直到下周末有事,帮不上忙.找找其它版主和坛友或自己多查查资料.
#17
huwenyi77582015-07-27 11:00
回复 16楼 lianyicq
好吧。thanks all the same
#18
huwenyi77582015-07-29 14:52
- -
1