【问题标题】:Word vba how to save picture from image object to file?Word vba如何将图片从图像对象保存到文件?
【发布时间】:2015-11-02 12:38:59
【问题描述】:

我在用户窗体上有一个图像对象。我想将该图像对象中的图片保存到文件中。我看到很多关于如何将图片加载到图像对象中的示例,但没有其他方法。

我试过stdole.SavePicture obj.Picture, strFilePath,但这只适用于按钮对象。

【问题讨论】:

  • 向我们展示您到目前为止所做的事情。什么不工作?
  • 有人有什么吗???

标签: image vba ms-word userform


【解决方案1】:

首先创建一个图表。第二把图片放在图表中。最后导出图表。

编辑#1

示例代码见:

Save Picture with VBA

【讨论】:

  • @Chuck 查看我的EDIT#1
  • 抱歉,我使用的是 WORD,而不是 Excel。并且图像对象在表单上,​​而不是在文档上(或 Excel 工作表上)。
  • 是的,您的适用于 Excel,因为它具有恰好可以满足我需要的图表对象。但 Word 没有。我一直在寻找答案,但很多 Excel 答案不断出现。感谢您的帮助!
【解决方案2】:

我不知道这是否有帮助,但我尝试将 Picture 对象转换为 IPictureDisp 对象以将其传递给 stdoleSavePicture 函数,它对我有用。

代码如下:

Dim pic As IPictureDisp

Set pic = myForm.Image1.Picture

stdole.SavePicture pic, "C:\myfile.jpg"

【讨论】:

    【解决方案3】:

    我使用 PowerPoint 来执行此操作。这是最近一个导出世界地图的项目的宏:

    Public Sub ExportMap()
    Dim pptPres As PowerPoint.Presentation
    Dim pptSlide As PowerPoint.Slide
    Dim pptShapeRange As PowerPoint.ShapeRange
    Dim Path$, File$
    Dim oRange As Range
    
      Application.ScreenUpdating = False
      myDate$ = Format(Date, "m-d-yyyy")
      Set pptApp = CreateObject("PowerPoint.Application")
      Path$ = ActiveDocument.Path & Application.PathSeparator
      File$ = "WorldMap " & myDate$ & ".png"
      Set pptPres = pptApp.Presentations.Add(msoFalse)
      
      Set oRange = ActiveDocument.Bookmarks("WholeMap").Range
      oRange.CopyAsPicture
      
      Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
      On Error Resume Next
      With pptPres.PageSetup
        .SlideSize = 7
        .SlideWidth = 1150
        .SlideHeight = 590
      End With
      Set pptShapeRange = pptSlide.Shapes.PasteSpecial(ppPasteEnhancedMetafile, Link:=msoFalse)
      
      pptSlide.Export Path$ & File$, "PNG"
      
      pptApp.Quit
      
      Set pptPres = Nothing
      Set pptApp = Nothing
      Set pptSlide = Nothing
      Application.ScreenUpdating = True
      MsgBox "All done! Check the folder containing this template for a file called '" & File$ & "'."
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 2017-07-28
      • 2012-02-04
      • 1970-01-01
      • 2020-05-23
      • 2022-12-11
      相关资源
      最近更新 更多