Solved

Visio 2013

Posted on 2013-11-07
2
656 Views
Last Modified: 2013-11-08
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
Comment
Question by:rogerdjr
2 Comments
 
LVL 11

Accepted Solution

by:
Visio_Guy earned 500 total points
ID: 39634197
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
 

Author Closing Comment

by:rogerdjr
ID: 39634670
Awesome - works perfect and you are right, this will get me started with visio

Thanks a bunch!!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever created a custom Visio stencil – a collection of your own unique master shapes – and then created a drawing by dragging masters onto the drawing page? Have you then made changes to the stencil master and wondered why the shapes on the …
The ability to add structure to Visio diagrams using containers, lists and callouts is one of my favorite features in Visio 2010. In this article we’ll examine lists. We’ll explore containers and callouts in separate articles. Prior to reading th…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now