简体   繁体   中英

Word VBA: Save as Picture

Does anyone know if it's possible to run the Word "Save as Picture" dialog from VBA?
Note: I've searched high and low to no avail, suggesting it just may not be possible.
Still, thought it worth at least asking.

Turns out the solution was there, just hard to find. While the control isn't on the ribbon, it still exists in the CommandBars. I managed to expose that by dumping out a list of all CommandBar controls (to find it and get the id). So, the solution is:

''' Note: Picture must be selected first
CommandBars.FindControl(ID:=5736).Execute

You can use PowerPoint to export the graphic. Here's a macro from a recent project that exports a map of the world. The Word graphics are EMF files:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM