VBA Macro to Copy Range and Paste range to PPT as Picture - vba

Need help to copy range of cells from excel sheet1 to PowerPoint file and paste it as Picture and make that picture fit inside the slide automatically.
I currently use Snagit software to take screenshot of the sheet and use send to powerpoint but it take lots of time.
Any feedback would be nice to automate this process

something like this
Sub BetterPicturePaste()
'...other stuff...
Windows(1).Sheets(1).Range("myrange").Copy
Windows(2).View.PasteSpecial DataType:=ppPasteEnhancedMetafile
End Sub

Related

Paste Excel Range to PowerPoint Slide in 2016 with Windows 10

I want to copy and paste an Excel Range into a PowerPoint slide. The range has conditional formatting, number formats and border formats. Here is what I have:
mwksTables.Range("T_Table").Copy
mobjPowerPointSlide.Shapes.PasteSpecial 0
This code works but doesn't paste keeping the original formatting. If I change the code to the following:
mobjPowerPointSlide.Shapes.PasteSpecial 2
it prints correctly but magnifies it so as to show only half of the data in the object (weird). It's like it zooms the range after copying but before pasting.
I have tried this:
Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
But this only works for Office 2010.
Can someone help? How can I paste a table into PowerPoint 2016 and make it look identical to the range in Excel 2016? Thanks!
Try pasting it as an OLE object...
oSlide.Shapes.PasteSpecial 10 'ppPasteOLEObject
Then you will also be able to either edit the worksheet object in PowerPoint or open it in Excel.

PowerPoint: Repeat text on every slide

I'm working on creating a PowerPoint template for daily class presentations. In the template I'd like to have a hunk of text that is prominently displayed on the first slide and which repeats at the bottom of the subsequent slides at the bottom in a smaller size. The text will change every day.
The ideas I've had so far:
Use a text field. As far as I can tell, PowerPoint doesn't have anything like a text field that can be dynamically set.
Use a footer - this works and I can modify the master to get the look I want, but I'd really like to be picking up the value of the text from the first page so that edits would be automatically applied and to save the initial step of setting the footer.
Using VBA - I'd be willing to give this a shot, but I've never used VBA and the learning curve seems steep so it would be nice to know if the idea is feasible in VBA.
Can this be done? How would you approach it?
Here's an example of what I'm hoping to be able to do. Ideally the solution would work on both the Mac (2013) and Windows (2016) version of PowerPoint.
You can connect your presentation with an excel file. And running the code in the ppt would pull out the text in the excel file and update the titles instantly.
Create a presentation with a textbox with some temporary text. Put the below code in ppt. save as pptm.
Sub AddMotionPath()
Dim Temp As String
Excel.Application.Workbooks.Open ("D:\Users\Desktop\Book1.xlsx") ' update the path of the excel file
Workbooks("Book1.xlsx").Activate 'activate the file
For p = 1 To 4
Temp = Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("B" & p + 1).Value ' Column B has the titles
ActivePresentation.Slides(p).Shapes(1).TextFrame.TextRange.Text = Temp ' this updates the titles from excel to ppt slide
Next
Excel.Application.Workbooks("Book1.xlsx").Close False 'closes the excel file
End Sub
Let me know if this works for you. You can update the excel file and run the macro in ppt. The text in the slides will be updated automatically.

How to use pastepecial to paste a chart as bitmap to another sheet in vba

Is there a way to use the pastspecial method to paste a copyied chart as a bitmap to another worksheet. Currently this is my syntax-
PasteSheet.PasteSpecial (Format:="Bitmap", Link:=False, DisplayAsIcon:=False)
Where PasteSheet is the other worksheet i want to paste to. Currently with this code, it is only pasting in the active sheet. Do i have to use select to copy then select the page i want to paste to, then change back to the sheet I copied from? I hope not as I have a lot of sheets haha.
Thank you
Edit: I have found out that if I Copy the chart as a shape rather than a chartobject I can use the pasteSpecial method to paste to another sheet. That being said it now pastes charts into one another creating one mega chart haha.
GraphSheet.Shapes(chtName).Copy
PasteSheet.PasteSpecial Format:="Microsoft Office Drawing Object", Link:=False , _
DisplayAsIcon:=False
This will work without needing to activate/select Sheet2:
Sheet1.ChartObjects(1).Chart.CopyPicture
Sheet2.Paste
Do i have to use select to copy then select the page i want to paste
to, then change back to the sheet I copied from?
Yes - the sheet you paste into must be active. Use Sheets("mytargetname").Select - just using Activate isn't enough...
If you set
Application.ScreenUpdating = False
your screen won't flash while you do this...

VBA to open Excel hyperlink does not work when hyperlink generated with a formula

There seems to be a bug with Excel hyperlinks which are generated with a formula. I'm using Excel 2010. I have a spreadsheet with cells containing URLs, and my goal is to do the following two things:
Turn these cells into hyperlinks.
Create a keyboard shortcut to open these hyperlinks so I don't have to use the mouse.
To do #1, initially I just used the function =HYPERLINK(). So, my URLs are in column A, and I used this formula to make hyperlinks in column B.
To do #2, I created the following macro which should open the hyperlink with the keyboard shortcut Ctrl+H:
Sub Open_Hyperlink()
'
' Open_Hyperlink Macro
'
' Keyboard Shortcut: Ctrl+h
'
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
The problem is that this macro only seems to work on hyperlinks which are not created using a formula. For example, if I just type into a cell http://www.google.com, Excel will automatically make this a hyperlink and the keyboard shortcut macro works, where it doesn't with formula generated hyperlinks.
I've also noticed that when I right click on formula generated hyperlinks, there is no option in the drop-down menu to open the hyperlink, yet there is that option when right clicking on hyperlinks not generated by a formula.
I've found the following workaround. Rather than generate hyperlinks using a formula, I used a macro which I found here.
Sub HyperAdd()
'Converts each text hyperlink selected into a working hyperlink
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell
End Sub
I'm able to use the keyboard shortcut to open the hyperlinks generated with this macro. I'm wondering if anyone has or had a similar problem, and why the formula generated hyperlinks are not working for me. I would prefer to use formulas to make hyperlinks in the future, since it is simpler, so if anyone knows of a way to avoid using a macro to make hyperlinks, I'd really appreciate it.
I'm wondering if anyone has had a similar problem, and why the formula
generated hyperlinks are not working for me.
Alas, this seems to be painful truth: Excel does not add to Hyperlinks collection formula-generated links - below is the screen from the debugger which is pointed to =HYPERLINK("http://www.google.com/";"Google"):
I'm not sure whether this is a deliberate implementation or a bug, but yes, formula-generated links may NOT be opened using Hyperlinks().Follow method.
However, if you're going to use keyboard shortcut for links opening, just use the following code - it will automatically convert to clickable link selected cell text and open it:
Sub Open_Hyperlink()
Selection.Hyperlinks.Add Anchor:=Selection, Address:=Selection.Formula
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
Just assign any shortcut and you're ready to go) Here is the sample: https://www.dropbox.com/s/d4cie7lun22quma/FollowLinks.xlsm
Hope that's somehow helpful. Good luck!

How can I copy and paste slides as a picture in PowerPoint using VBA?

I want to cut one slide and paste it as a picture in same presentation file (ppt format).
I know that following VBA code work for copy&paste in a single slide.
ActivePresentation.Slides(1).Copy ''copy first slide into clipboard
ActivePresentation.Slides.Paste ''paste above slide as a last slide
What I want to know is how to paste a slide as a "picture". ('paste as a picture' is an option of Paste Special [e.g. paste as a PNG,JPEG...])
Are there any suggestions for how to go about this?
Yeah, your code was pretty close. Here's an example of taking Slide 1 and pasting it as a picture in Slide 2.
ActivePresentation.Slides(1).Copy
ActivePresentation.Slides(2).Shapes.PasteSpecial ppPasteJPG
You can look up PpPasteDataType for more formats to paste to.