Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Visio 2013

Posted on 2013-11-07
2
671 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent Visio zone question asked how to use the data in an Excel file to create a Visio Gantt chart -- and then to dynamically update the Gantt chart. The good news is that the Viso Gantt Chart wizard can be automated. The bad news is that the…
Meetings to discuss business process can waste time, and often do .  The meeting's dialog can get confusing when participants have different professional perspectives and backgrounds.  A jointly-developed process picture helps wade through the confu…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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