View Single Post
 
Old 05-05-2012, 02:45 PM
CatMan CatMan is offline Windows 7 32bit Office 2010 32bit
Intermediate
 
Join Date: Apr 2012
Posts: 39
CatMan is on a distinguished road
Default Title: Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur

Hello forum, thank you for taking a look at this problem,

This code inserts two pictures using .addPicture, the 1st iteration resizes and centers MyPic1, the 2nd iteration inserts MyPic2 but then resizes and centers MyPic1 (again) failing to change focust to MyPic2.

To further complicate things, this code operates on both pictures on one PC and fails to operate on MyPic2 on another PC, both PC's are Win 7 with Office 2007. 'I am hoping the code below will fail for you on the 2nd iteration (thus demonstrating the problem) but if it does not then it leaves me to speculate what the problem might be. My best guess is, after the code adds MyPic2 (2nd iteration of .addPicture) the next code line 'oPSe.Shapes.Count' is the problem, it fails to change focus to MyPic2 and instead remains focused on MyPic1 but I do not know how to force focus to MyPic2 (e.g. the latest .addpicture object). Well that's just my guess for what its worth.

Regarding the PowerPoint referenced in the code, all you need to do is save a PowerPoint with one blank slide before you run the code.


Code:
Sub TestCode()
 
'Open Existing PowerPoint
Set oPAe = CreateObject("PowerPoint.Application")
With oPAe
    .Visible = True
    Set oPPe = .Presentations.Open("C:\Users\User\Documents\Test\Test.pptx")
End With
 
'Insert two pictures (2nd iteration fails)
MyPicture = "C:\Users\User\Documents\Test\MyPic1.jpg": GoSub InsertPicture 'MyPic1 is inserted, resized and centered (this iteration is ok)
MyPicture = "C:\Users\User\Documents\Test\MyPic2.jpg": GoSub InsertPicture 'MyPic2 inserts but resize and centering is applied to MyPic1 (this iteration fails)
Exit Sub
 
InsertPicture:
With oPAe
    .ActiveWindow.View.GotoSlide (1) 'this line makes testing easier otherwise not required
    Set oPSe = oPPe.Slides(1)
   'Add Picture
    Set oShapee = oPSe.Shapes.AddPicture(MyPicture, msoFalse, msoTrue, 1, 1, -1, -1) '(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)
 
   'Do some work (code works below here but operates on MyPic1 both iterations)
   'Size Picture
    Set oPicturee = oPSe.Shapes(oPSe.Shapes.Count)
    oPicturee.LockAspectRatio = True
    oPicturee.Width = 500 'value is arbitrary
   'Center picture
    With oPAe.ActivePresentation.PageSetup
        oPicturee.Left = (.SlideWidth \ 2) - (oPicturee.Width \ 2)
        oPicturee.Top = (.SlideHeight \ 2) - (oPicturee.Height \ 2)
    End With
End With
Return
 
End Sub
Reply With Quote