Results 1 to 33 of 33

Thread: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in middle)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in middle)

    Hi There,

    I have written a code for work in VBA (I am a beginner) and would like to take content from each slide and paste the slide content as a PNG. Essentially the intent is to "Lock" the deck so that anyone who has access cannot modify the information.

    Anyway, the code works great in that it will select all> cut all information>delete anything left on the slide>paste as PNG. However, I have not been able to figure out how to paste in the original position that everything was in. By default I set a paste in the middle. When I have a deck that is 170 slides, it can be annoying to have to review the position of each and every slide.

    My guess is that I may need to set a reference point when I first highlight everything, but I have no idea how to do this and utilize this position as a spot to paste.

    Here is my code:

    Sub test()
    Dim sld As Slide
    Dim pre As Presentation

    Set pre = ActivePresentation

    For Each sld In pre.Slides
    sld.Shapes.Range.Cut

    If sld.Shapes.Count > 0 Then
    sld.Shapes.Range.Delete
    sld.Shapes.PasteSpecial ppPastePNG
    ' Need to figure out how to paste in place here
    sld.Shapes.Range.Align msoAlignCenters, msoTrue
    sld.Shapes.Range.Align msoAlignMiddles, msoTrue
    End If

    If sld.Shapes.Count = 0 Then
    sld.Shapes.PasteSpecial ppPastePNG
    ' Need to figure out how to paste in place here
    sld.Shapes.Range.Align msoAlignCenters, msoTrue
    sld.Shapes.Range.Align msoAlignMiddles, msoTrue
    End If

    Next
    End Sub

    I appreciate the help!!

  2. #2
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Perhaps the effect will be more appealing if you copy the whole slide?
    Something like this:
    Code:
       Dim sld As Slide
       Dim pre As Presentation
    
       Set pre = ActivePresentation
       
       If pre.Slides(2).Shapes.Count > 0 Then pre.Slides(2).Shapes.Range.Delete
          
       pre.Slides(1).Copy
       pre.Slides(2).Shapes.PasteSpecial ppPasteJPG
       With pre.Slides(2).Shapes(1)
          .Left = 0
          .Top = 0
          .Width = pre.Slides(2).Master.Width
          .Height = pre.Slides(2).Master.Height
       End With
    Note that I copied from slide1 to slide2.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Hmmm, I would copy the whole slide, but there are many instances where the slide has either comments OR hyperlinks. At this point it would be faster to go through each slide and readjust the pasted PNG rather than copying the entire slide.

  4. #4
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Since....
    the PasteSpecial method returns a ShapeRange object representing the shape range that was pasted.
    You can work with Left and Top properties.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    ^3com - that was what I was thinking about: when I initial select all the shapes, I can identify the farther left and top corner point as a position, and then reference this position when doing the pasting. However, I have no idea how to identify or code this position...

  6. #6
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    I can identify the farther left and top corner point as a position
    Then store those values into variables and pass them to left and top properties. Something like.....
    iniLeft= ' The value you can get
    iniTop= 'Idem

    With Windows(1).Selection.ShapeRange(1)
    .left=iniLeft
    .top=iniTop
    End With

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Hmmmm ok, I understand. Once I do the select-all code, would I need to add a Dim feature right after, so that each slide has its own unique Dim feature?

    Dim sld as Slide
    Dim pre as Presentation

    set pre = activepresentation

    For each sld in pre.slides
    sld.shapes.selectall
    'Add code here to define what top position is (eg. Dim sld.shapes.select.shaperange.top as initop)
    'Add code here to define what left position is (eg. Dim sld.shapes.select.shaperange.left as inileft)

    sld.shapes.range.cut

    If sle.shapes.count > 0 Then
    sld.shapes.range.delete
    sld.shapes.paste special ppPastePNG
    sld.shapes.range.align inileft
    sld.shapes.range.align initop
    End If

    If sld.shapes.count = 0 Then
    sld.shapes.pastespecial ppPastePng
    sld.shapes.range.align inileft
    sld.shapes.range.align initop
    End If
    Next
    End Sub

  8. #8
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    this code does work for you? try it and let us know.
    If you loop through all shapes, you can capture Left and Top, and stor those value into variables, after use those variables to set new Left and Top. Since I do no see slides, only can imagine scenario.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Unfortunately the code does not work for me - I keep getting a compiling error that highlights ".top" in the set initop portion. Not too sure if my code is defining the position of the slide....Here is my attend at the code to set the variables:

    Name:  bug.png
Views: 1645
Size:  13.8 KB

  10. #10
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    your set statement should be more like:

    Code:
    iniTop = sld.Shapes(1).Top

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    ^vbfbryce - Nope, still doesn't work. Still identifies a compile error at the ".top" portion.

  12. #12
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Bryce, you are right.

    Well, try this one....

    Code:
    Sub test()
    Dim i as integer
    Dim sld As Slide
    Dim pre As Presentation
    Set pre = ActivePresentation
    
    For i = 1 To pre.Slides.Count
        initop = pre.Slides(i).Shapes(1).Top
        inileft = pre.Slides(i).Shapes(1).Left
        pre.Slides(i).Select
        pre.Slides(i).Shapes.SelectAll
        pre.Slides(i).Shapes.Range.Cut
        pre.Slides(i).Shapes.PasteSpecial ppPastePNG
        pre.Slides(i).Shapes(1).Top = initop
        pre.Slides(i).Shapes(1).Left = inileft
    Next i
    
    End Sub
    HTH

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Hmmm, still doesn't work. It seems that it is reconizing "top" and "left" as the top-left of the actually slide-deck itself (see the attached picture), not the top-left of the shape selection.Name:  code test.jpg
Views: 1545
Size:  31.4 KB

  14. #14
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    It works for me. The only difference is than I used 3 blank slides(disseny), to test it.
    Did you tried my code without add anything?

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    ^3Com - I' m not too sure what you mean by trying code without add anything and using blank slides? Are you testing with content in the deck?

    Also, is there a reason why you are using slides(i).shapes(1) as opposed to slides(i).shapes???

  16. #16
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    you can't use "shapes" because that is an arrary (collection?). you have to give it an index (x).

  17. #17
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    try the attached example
    Attached Files Attached Files

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Hmmm, ok, I may need a layman's explaination for what an array is, but in any case, would there be a reason why it is not recognizing the indicated position of the highlighted shapes? It seems that even when I delete "pre.Slides(i).Shapes(1).Top = initop" and "pre.Slides(i).Shapes(1).Left = inileft", the code goes through but pastes the image in the exact same position in the top left-hand corner. It is as if it is not recognizing the values of initop or inileft.

    Am I missing something key here?

  19. #19
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    if you run the code in "break mode," what are the values of "initop" and "inileft" at that point?

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    What is interesting is that when I choose .top = 100 or .left = 100, the image is pasted in this exact location, but when I choose initop or inileft, the value isn't recognized.

  21. #21
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    again, what are the values of "initop" and "inileft" when you run in break mode?

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Apologies, but I have no idea where/what break mode is. Does it refer to the "pause button" near the top of the toolbar?

    When I press the run button, press Ctrl+Break does not indicate the values of initop or inileft either. How exactly would I find the values via Break Mode?
    Name:  test1.jpg
Views: 1834
Size:  24.2 KB

  23. #23
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    put the word "stop" after your "For i..." line, then the code will pause when it hits that line. You can step through line by line by using the F8 key. When you hover over a variable, it will show its value at that time.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    AH ok, gotcha.

    In the first two lines with "initop" and "inileft", the values are 18 and 35.704.

    In the last 2 lines with "pre.slides...... = initop" and "pre.slides...... = inileft", the values are 185.625 and 54.

  25. #25
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    so it should be pasting there, then...

  26. #26
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Sheesh!! this got busy.

    Quote Originally Posted by matt.wrobel View Post
    Hmmm, I would copy the whole slide, but there are many instances where the slide has either comments OR hyperlinks. At this point it would be faster to go through each slide and readjust the pasted PNG rather than copying the entire slide.
    I have not observed that comments get copied into the image, but your statement about the hyperlinks was a curve ball. It took a bit of research, but I think I got that figured out (maybe ).

    Here is my test code. It is limited to only processing the first slide, but nothing prevents you from putting it in a loop to process all slides.

    VBA Code:
    1. Sub SlideToImages()
    2.  
    3.    Dim pre As Presentation
    4.    Set pre = ActivePresentation
    5.    Dim sld As Slide
    6.    
    7.    Dim shp As Shape
    8.    Set sld = pre.Slides(1)
    9.    
    10.    Dim pasteshp As Shape
    11.  
    12.    Dim i As Integer
    13.    For i = sld.Shapes.Count To 1 Step -1
    14.    
    15.       Set shp = sld.Shapes(i)
    16.       shp.Copy
    17.       Set pasteshp = sld.Shapes.PasteSpecial(ppPasteJPG).Item(1)
    18.       With pasteshp
    19.          .Top = shp.Top
    20.          .Left = shp.Left
    21.          .Width = shp.Width
    22.          .Height = shp.Height
    23.          Call CloneActionSettings(shp, pasteshp, ppMouseOver)
    24.          Call CloneActionSettings(shp, pasteshp, ppMouseClick)
    25.       End With
    26.       shp.Delete
    27.      
    28.    Next i
    29.    
    30.    sld.Layout = ppLayoutBlank ' prevent automatic shapes from repopulating
    31.    
    32. End Sub
    33.  
    34. Private Sub CloneActionSettings(sourceShape As Shape, destinationShape As Shape, ppmouse As PpMouseActivation)
    35.    Dim dest As ActionSetting
    36.    Set dest = destinationShape.ActionSettings(ppMouseClick)
    37.    Dim src As ActionSetting
    38.    Set src = sourceShape.ActionSettings(ppMouseClick)
    39.    dest.Action = src.Action
    40.    dest.AnimateAction = src.AnimateAction
    41.    If src.Action <> ppActionNone Then
    42.       On Error Resume Next
    43.    
    44.       Select Case src.Action
    45.          Case ppActionEndShow
    46.          Case ppActionFirstSlide
    47.          Case ppActionHyperlink
    48.             dest.Hyperlink.Address = src.Hyperlink.Address
    49.             dest.Hyperlink.EmailSubject = src.Hyperlink.EmailSubject
    50.             dest.Hyperlink.ScreenTip = src.Hyperlink.ScreenTip
    51.             dest.Hyperlink.ShowAndReturn = src.Hyperlink.ShowAndReturn
    52.  
    53.             dest.Hyperlink.SubAddress = src.Hyperlink.SubAddress
    54.             dest.Hyperlink.TextToDisplay = src.Hyperlink.TextToDisplay
    55.          Case ppActionLastSlide
    56.          Case ppActionLastSlideViewed
    57.          Case ppActionMixed
    58.          Case ppActionNamedSlideShow
    59.             dest.SlideShowName = src.SlideShowName
    60.          Case ppActionNextSlide
    61.          Case ppActionOLEVerb
    62.             dest.ActionVerb = src.ActionVerb
    63.          Case ppActionPlay
    64.          Case ppActionPreviousSlide
    65.          Case ppActionRunMacro, ppActionRunProgram
    66.             dest.Run = src.Run
    67.             dest.AnimateAction = src.AnimateAction
    68.          
    69.       End Select
    70.       On Error GoTo 0
    71.    End If
    72.  
    73. End Sub

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Name:  Test2.jpg
Views: 1484
Size:  8.8 KB

    Hmmmm, the code still doesn't work - it doesn't even seem to convert the first slide as a PNG. Also, after going through some of the slides of my presentation it looks like it keeps setting initop and inileft with different positions (ie. sometimes it pastes the PNG in the bottom of the screen, sometimes at the top left, sometimes closer to the right). It is quite varied.

    I think there is a bug that is occurring when the position of initop and inileft is being defined. Eg. If there are 3 shapes (see above), which shape defines top and left? Is it the one of the middle left or it is the one on the top right. I think this is throwing things off.

    I'll attached some slides tomorrow to show you what mean.

  28. #28
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    I think there is a bug that is occurring when the position of initop and inileft is being defined. Eg. If there are 3 shapes (see above), which shape defines top and left? Is it the one of the middle left or it is the one on the top right. I think this is throwing things off.
    You loop through each shape and capture their left+top properties storing them in two variables.
    After that you copy & paste each one in the same position it was before, using for that the variables storing the old shape position.

    Because there are 3 shapes, yo do...

    for i = 1 to 3 ' If you ignore how many shapes are there, then you use: shapes.count
    ' now we store left and top in a variable
    inileft=shapes(i).left
    inTop=shapes(i).top

    'Now that we've those values, we lets cut and paste shape
    Shapes(i).Range.Cut
    shapes(i).pastespecial ppPastePng

    'Now we've the shapes pasted, but not the right position. We force it to old position.
    shapes(i).left=inileft
    shapes(i)top=iniTop

    Notice till now we are working with shape 1. In the next iteration we'll work with the next shape, and so on...
    In this way, we cut, paste and positioning one by one all the shapes.

    Did you get the point now?

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    ^3Com - I see what you mean, but the issue here is that I need to do a cut of all the shapes at once and then paste as a single PNG, not as separate small PNGs. That is why I am recognizing the error of utilizing which shapes as the reference point.

    It seems that when I copy and paste an image from one powerpoint to a new powerpoint file, the original location is preserved, but when I copy and paste within the same powerpoint file, this is where the off-set occurs. I think if I can figure out the code that retains the position from one powerpoint to the other, I would be in great shape.

  30. #30
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    but the issue here is that I need to do a cut of all the shapes at once and then paste as a single PNG, not as separate small PNGs
    http://msdn.microsoft.com/en-us/libr...ice.14%29.aspx

    Try this one...

    Code:
    Sub test1()
    
    Dim sld As Slide
    Dim pre As Presentation
    Set pre = ActivePresentation
    
    For i = 1 To pre.Slides.Count
        pre.Slides(i).Select
        pre.Slides(i).Shapes.SelectAll
        inileft = Windows(1).Selection.ShapeRange.Left
        initop = Windows(1).Selection.ShapeRange.Top
        Windows(1).Selection.ShapeRange.Cut
        pre.Slides(i).Shapes.PasteSpecial ppPastePNG
        pre.Slides(i).Shapes(1).Top = initop
        pre.Slides(i).Shapes(1).Left = inileft
    Next i
    
    End Sub
    HTH

  31. #31

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Nope, still doesn't work. After running it in break mode, the position of left and top (relative to the image below after doing a Select All) are both valued at "-2.147484E+09".

    It gives me a Runtime error "-2.147024809 (80070057).... the specified value is out of range".

    THis error seems to occur even when I delete particular slides.

    Name:  testslide.jpg
Views: 1452
Size:  13.5 KB

  32. #32
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    When you discounted my original solution and mentioned the hyperlinks, I thought you meant that you wanted to preserve that functionality. That actually made sense. However, it is apparent now that you want to convert each slide to a single image that destroys all transitions and other features.

    The following is based on 3Com's suggestion, but computes the most top-left position occupied by the shapes on the slide. This is the coordinate that you want.

    VBA Code:
    1. Dim sld As Slide
    2.    Set sld = ActivePresentation.Slides(1)
    3.    
    4.    Dim sr As ShapeRange
    5.    Set sr = sld.Shapes.Range
    6.  
    7.    If sr.Count > 0 Then
    8.       ' determine the most Top-Left coordinate of all the shapes
    9.      
    10.       Dim x As Long
    11.       Dim y As Long
    12.       ' initialize comparison parameters to 1st shape
    13.       x = sr.Item(1).Left
    14.       y = sr.Item(1).Top
    15.      
    16.       ' compare to remaining shapes in collection
    17.       Dim i As Integer
    18.       For i = 2 To sr.Count
    19.          If sr.Item(i).Top < y Then
    20.             x = sr.Item(i).Left
    21.             y = sr.Item(i).Top
    22.          End If
    23.       Next
    24.      
    25.       sr.Cut
    26.       Set sr = sld.Shapes.PasteSpecial(ppPasteJPG)
    27.      
    28.       ' set position
    29.       sr.Left = x
    30.       sr.Top = y
    31.       sld.Layout = ppLayoutBlank ' prevent automatic shapes from repopulating
    32.    End If

    However let's go back to your original stated goal.
    Essentially the intent is to "Lock" the deck so that anyone who has access cannot modify the information.
    Why not just make the presentation Read-Only? This way you preserve all the work that the presentation's creator put into defining transitions and such. If anyone did what this will do to one of my presentations, well lets say I would not be very pleased with them and they would very well know that fact.

    Additionally, converting the slide to an image in no way prevents someone from altering that image. They can still save/edit/replace it. I know that a password lock on modifications is also not full-proof, but at least it does not muck with the presentation.

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    18

    Re: Powerpoint Macro - Cannot figure out how to Paste in Place (can only paste in mid

    Yes, I'm some instances I would actually need to manually reinsert the hyperlinks (ie. paste the PNG image, then add on top a shape that is transparent and links to a part of the slide). However, yes, at the moment my main concern is simply pasting the information as a single PNG in it's original location.

    To help understand where I am coming from with the request, I work in the healthcare industry and there are instances where we would need to present sensitive information to individuals that, if modified without us knowing, could lead to some huge issues. Eg. someone takes the information, modified it a bit, is perceived in the wrong way, we get in trouble because someone was negatively affected as a result. Locking as a PNG and having a read-only is a necessary risk-mitigation step.

    Here is the reason why I can't do read-only: sometimes my colleagues need to create their own slideshows, but need information from particular decks. Now they do have access to read-only versions of the decks through our in-house system, but if for any reason they need to go ahead and move slides around or cut slides out or do something else so that they can tailor the information to whomever they are presentating the information to, we need to give them an option to this hence locking the PNG.

    I'll play around with the code a bit, but from a first glance it works great other than shifting things just a bit to the right... (about 2 cm)
    Name:  test.jpg
Views: 1457
Size:  20.3 KB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width