Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-05-2012, 02:45 PM
CatMan CatMan is offline Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Windows 7 32bit Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Office 2010 32bit
Intermediate
Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur
 
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
  #2  
Old 05-05-2012, 11:18 PM
JohnWilson JohnWilson is online now Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Windows 7 64bit Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

Don't see why that doesn't work but I think I would do it like this:

Code:
Sub TestCode()
Dim objPPTApp As Object
Dim objPPPres As Object
Dim objPPSld As Object
'Open Existing PowerPoint
Set objPPTApp = CreateObject("PowerPoint.Application")
With objPPTApp
    .Visible = True
    Set objPPPres = .Presentations.Open("C:\Users\John\Documents\Test\Test.pptx")
End With
Set objPPSld = objPPPres.Slides(1)
Call addpic(objPPPres, objPPSld, "C:\Users\John\Documents\test\Pic1.jpg")
Call addpic(objPPPres, objPPSld, "C:\Users\John\Documents\test\Pic2.jpg")
End Sub

Sub addpic(opres As Presentation, osld As Slide, strPath As String)
Dim opic As Shape
Set opic = osld.Shapes.AddPicture(strPath, False, True, 1, 1, -1, -1)
With opic
.LockAspectRatio = True
.Width = 500
With opres.PageSetup
        opic.Left = (.SlideWidth \ 2) - (opic.Width \ 2)
        opic.Top = (.SlideHeight \ 2) - (opic.Height \ 2)
End With
End With
End Sub
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
  #3  
Old 05-06-2012, 09:27 PM
CatMan CatMan is offline Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Windows 7 32bit Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Office 2010 32bit
Intermediate
Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur
 
Join Date: Apr 2012
Posts: 39
CatMan is on a distinguished road
Default

Hello JohnWilson again, thank your for your continued help, I really appreciate it. I ran the code you offered and am getting a type mismatch error on the line containing .addPicture. Even with this error the picture is inserted into slide 1 but then it stops. I can't figure out what is causing the mismatch, objPPTAAppa is the PowerPoint Application object, objPPPres is the specific PowerPoint file, looks like objPPSid is the ID of slide 1, that all makes sense to me, not sure what is causing the mismatch.
Reply With Quote
  #4  
Old 05-07-2012, 03:42 AM
JohnWilson JohnWilson is online now Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Windows 7 64bit Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

Are you running the vba in PowerPoint itself or somewhere else (Word / Excel maybe)??
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
  #5  
Old 05-07-2012, 07:40 AM
CatMan CatMan is offline Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Windows 7 32bit Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Office 2010 32bit
Intermediate
Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur
 
Join Date: Apr 2012
Posts: 39
CatMan is on a distinguished road
Default

Running in Excel, I should have mentioned that.
Reply With Quote
  #6  
Old 05-07-2012, 07:47 AM
JohnWilson JohnWilson is online now Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Windows 7 64bit Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

Quote:
Originally Posted by CatMan View Post
Running in Excel, I should have mentioned that.
Yep, that's the problem.

Unless you have set a reference to powerPoint Excel has no idea what a Presentation or a Slide are and will be confused by "Shape"

In the second sub routine change these to object

Sub addpic(opres As Object, osld As Object, strPath As String)
Dim opic As Object
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
  #7  
Old 05-10-2012, 10:25 PM
CatMan CatMan is offline Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Windows 7 32bit Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Office 2010 32bit
Intermediate
Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur
 
Join Date: Apr 2012
Posts: 39
CatMan is on a distinguished road
Default

Thanks John, your code suggestion and the change to make it work with Excel VBA totally fixed the problem. Many thanks again, this really helped. Can't say enough good things about this forum.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Title:  Why does focus fail to change to MyPic2.jpg (intermittently) after .addPictur Fail to access outlook to set the settings!!!!! Jamal NUMAN Outlook 3 04-20-2011 06:32 PM
Duration changes fail to update work effort Panteledes Project 6 05-25-2010 02:27 PM
Secondary IP address Outlook Fail-over danish.mustafa Outlook 0 03-01-2010 01:32 AM
Help with forms (shifting focus) jrh312 Word 0 11-19-2009 12:59 PM
Shortcuts Fail sussertown Outlook 0 02-16-2009 06:38 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft