• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 706
  • Last Modified:

Visio 2013

Looking for a vba macro for visio that will import (several) jpg files to use as tracing backgrounds.

Ideally, it would page through a directory insert a jpg and save it to a visio tab (page) as a background.

I've used vba a lot with other applications but never with Visio so even a place to start will help.
0
rogerdjr
Asked:
rogerdjr
1 Solution
 
Visio_GuyCommented:
Hi Roger,

This isn't a perfect macro, and it doesn't deal with the file system, but it shows you how to create a page, import an image, and resize + center the image on the page.

There's plenty to learn from the sample, good luck!

Public Sub AddPageAndImportImageExample()

  Dim pg As Visio.Page
  Set pg = Visio.ActiveDocument.Pages.Add()
  
  pg.Name = "Example" & pg.Document.Pages.Count '//...note: must be unique for the document!
  
  '// Import an image to the page, and get it
  '// as a shape object:
  Dim shp As Visio.Shape
  Call pg.Import("D:\Images\SomeImage.jpg")
  Set shp = pg.Shapes(pg.Shapes.Count) '//...ie: the top-most shape, which was just added
  
  '// Make the shape fit the page. We'll need to calculate
  '// how much bigger or smaller the image is, relative
  '// to the page:
  Dim pw As Double, ph As Double
  pw = pg.PageSheet.CellsU("PageWidth").ResultIU
  ph = pg.PageSheet.CellsU("PageHeight").ResultIU
  
  Dim w As Double, h As Double
  w = shp.CellsU("Width").ResultIU
  h = shp.CellsU("Height").ResultIU
  
  Dim f As Double, fw As Double, fh As Double
  fw = w / pw
  fh = h / ph
    
  If (fw > fh) Then
    f = 1 / fw
  Else
    f = 1 / fh
  End If
  
  '// Change the size of the image by f:
  shp.CellsU("Width").ResultIU = shp.CellsU("Width").ResultIU * f
  shp.CellsU("Height").ResultIU = shp.CellsU("Height").ResultIU * f
  
  '// Center the image-shape on the page
  shp.Cells("PinX").FormulaForceU = "ThePage!PageWidth*0.5"
  shp.Cells("PinY").FormulaForceU = "ThePage!PageHeight*0.5"
  
  '// Cleanup:
  Set pg = Nothing
  Set shp = Nothing
  
End Sub

Open in new window

0
 
rogerdjrAuthor Commented:
Awesome - works perfect and you are right, this will get me started with visio

Thanks a bunch!!!!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now