Home > database >  Word VBA: Save as Picture
Word VBA: Save as Picture

Time:10-27

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.

CodePudding user response:

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

CodePudding user response:

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
  • Related