Copying multiple images from one workbook to another as separate images - image

In 2003 the code worked perfectly, we just updated to 2010 and its affecting our outgoing proposals.
I've been looking on multiple sites and everything i have tried gives me all the pictures pasted into one grouped image OR gives me multiple boxes that say image cannot be viewed.
The pictures will always be located in column L, but it can be one picture or 50 and even none. So i need to be able to select all images, copy and open another workbook and paste in a designated column with the same format and as separate images, not as a single image which is what I am getting right now. Any help would be greatly appreciated. Below is the latest code I have tried, still getting a "single grouped image" when pasting.
Windows(ourName2).Activate
Sheets("Sheet5").Select
On Error Resume Next
ActiveSheet.Pictures.Copy
Windows("Proposal.xls").Activate
Sheets("Sheet2").Select
ActiveSheet.PasteSpecial Range("L7")
Update, attempting to use this code raises error on the line Set wbSource = Workbooks("ourName2")
Dim wbSource As Workbook
Dim wbDest As Workbook
Dim shSource As Worksheet
Dim shDest As Worksheet
Dim shp As Shape
Set wbSource = Workbooks("ourName2") 'modify as needed
Set wbDest = Workbooks("MPlanner.xls") 'modify as needed
Set shSource = wbSource.Sheets("Sheet5") 'modify as needed
Set shDest = wbDest.Sheets("MAudit") 'modify as needed
shSource.Pictures.Copy shDest.Range("L7").Paste

This worked for me:
Sub test()
ActiveSheet.Pictures.Copy
With Workbooks("temp.xls").Sheets("Sheet1")
.Parent.Activate
.Activate
.Range("L7").Select
.Paste
End With
End Sub

Echoing Tim, this worked for me, resulting in non-grouped pictures. There should not be any reason you need to Activate the respective sheets.
The problem seems to be that you were using PasteSpecial method instead of Paste. I have a 2003 box at home I could verify on, but on 2010 Excel, the PasteSpecial method pasts the multiple pictures as a single object, whereas Paste puts them each individually.
Sub CopyAllPictures()
Dim wbSource As Workbook
Dim wbDest As Workbook
Dim shSource As Worksheet
Dim shDest As Worksheet
Dim shp As Shape
Set wbSource = Workbooks("Book12") 'modify as needed
Set wbDest = Workbooks("Book13") 'modify as needed
Set shSource = wbSource.Sheets("Sheet1") 'modify as needed
Set shDest = wbDest.Sheets("Sheet1") 'modify as needed
shSource.Pictures.Copy
shDest.Range("L7").Paste
End Sub

Related

How do I add a hyperlinks to image using VBA?

I have a script that will run on GPO on Windows Server 2012.
It's pretty simple stuff, but I cannot fathom how to add a hyperlink around an image! I have:
objSelection.InlineShapes.AddPicture "linktoimage.html"
This works a dream, I can see the image and there is no issue. But how do I add a hyperlink to this image so that when folks click on it they are taken to my desired hyperlink location. I know adding an image is simply a line of code, hoping for same for adding a hyperlink.
I am not doing this in excel or anything of the kind, just Notepad++
Assuming this is about Word, you need to save a reference the shape you just created and then use that reference as an argument to Document.Hyperlinks.Add
Option Explicit
Sub LinkImageTest()
Dim oSelection As Selection
Dim oDocument As Document
Dim oShape As InlineShape
Set oSelection = Application.Selection
Set oDocument = oSelection.Document
Set oShape = oSelection.InlineShapes.AddPicture("https://yt3.ggpht.com/-Pde_zs2tuj0/AAAAAAAAAAI/AAAAAAAAAAA/iBq9KSwTTLk/s88-c-k-no-mo-rj-c0xffffff/photo.jpg")
oDocument.Hyperlinks.Add oShape, "http://www.microsoft.com"
End Sub

Using common dialog to save picturebox as image vb6

I am using the common dialog control to save a picturebox in my form as an image. However, when I test it out, the bmp file that is produced is blank.
I am using the line function to draw lines on the picutrebox. This is what I want to save.
My code:
Private Sub mnuFileSave_Click()
Dim FileName As String
savedlg.FileName = ""
savedlg.Filter = "Bitmap files (*.bmp)|*.bmp|"
savedlg.ShowSave
If savedlg.FileName = "" Then Exit Sub
picGraph.Picture = picGraph.Image
SavePicture picGraph.Picture, savedlg.FileName
End Sub
Any help as to why the saved bmp file is coming up blank would be appreciated, thanks.
Edit: I have also found that the picture box goes blank when the picture is saved as an image.
Answer is simple:
AutoRedraw on the picture box must be set to true. It was set to false before.

Copy / Paste from other workbooks

I wrote code to copy and paste to my workbook the used ranges from other workbooks. In my computer it works but when I send to anyone the paste process results in an error message:
"This image cannot currently be displayed."
The currently version, I used xlPasteAll:
Workbooks(fl.Name).Worksheets(sheetindex).Range("A2:P" & Lastrow).Copy
Workbooks(fl.Name).Close
Worksheets(sheetindex).Activate
ActiveSheet.Range(Cells(startrow,1),Cells(rangeCount,16)).PasteSpecial xlPasteAll
You might want to have a look at this KB-Article.
It describes an error which occurs with images from other workbooks while copying a sheet or range into an other workbook.
Had a similar problem - we copied a sheet with images into the target workbook. If you close the source workbook before you save and close the target workbook this error would occur.
If you left the source workbook open and closed the target workbook, the image was shown correctly (Close source workbook afterwards and reopen the target workbook manually).
The KB-Article/Hoftix was able to fix our problem - it would also explain why it works on your Workstation and not somewhere else. (Different office patches...)
You are closing the sheet after copy the values. use close command after pasting the values
Sub test()
Workbooks(fl.Name).Worksheets(sheetindex).Range("A2:P" & Lastrow).Copy
Worksheets(sheetindex).Activate
ActiveSheet.Range(Cells(startrow, 1), Cells(rangeCount, 16)).PasteSpecial xlPasteAll
Application.CutCopyMode = False
Application.CutCopyMode = True
Workbooks(fl.Name).Close
End Sub

Deleting shape in PowerPoint programatically and undoing causes shape to be "lost"

(This seems mostly to be a PowerPoint 2007 specific problem, which I can't easily reproduce in PPT 2010)
Easy way to reproduce locally is to:
1) Insert a shape into blank slide
2) Run command: ActivePresentation.Slides(1).Shapes(1).Delete in immediate window in Visual Studio. (You can alternatively delete through C#)
3) Undo the deletion in the PowerPoint presentation (do this non-programatically)
For some reason, you cannot access the shape again using calls like:
ActivePresentation.Slides(1).Shapes(1) //Does not allow any methods/properties to work
The only thing I've remotely gotten is that through Selection.ShapeRange, you can kind of get a reference to the item, but most of the Properties/Methods throw ComExceptions when trying to using that object.
Does anyone know how I can re-get the shape or somehow refresh the presentation to get some clean Com Objects?
I can confirm in Ppt2007 SP3. Try as workaround .cut and .paste. Afterwards I was able to access the other methods/properties. - cheers, www.MSO-dlx.com
ActivePresentation.Slides(1).Shapes(1).Delete
Application.CommandBars.ExecuteMso "Undo" 'or manually Undo
ActiveWindow.Selection.SlideRange(1).Shapes(1).Cut
ActiveWindow.Selection.SlideRange(1).Shapes.Paste
ActiveWindow.Selection.SlideRange(1).Shapes(1).TextFrame.TextRange.Text = "ABC"
so, I can confirm that this is an issue; even in 2010 and found a "cheaper" alternative:
Public Sub arf()
Dim arf As Slide
Dim shape As shape
Dim shapes As shapes
Set shapes = ActivePresentation.Slides(1).shapes
Set shape = shapes(2)
shape.Select
shape.Delete
Application.CommandBars.ExecuteMso "Undo"
MsgBox ("shape: " & shape.Name & ",Type: " & shape.Type)
Set shapes = ActivePresentation.Slides(1).shapes
Set shape = Nothing
Set shape = shapes(2)
' Cut and paste makes this work, but not required...
'shape.Select
'shape.Cut
'shapes.Paste
'Set shape = Nothing
'Set shape = shapes(2)
Set arf = shape.Parent
MsgBox ("slide: " & arf.Name)
End Sub
Just now I have successfully solved the problem, share the web page which helped a lot : https://www.add-in-express.com/creating-addins-blog/2014/06/24/exception-hresult-0x800a01a8/
The critical point is releasing the object after deleting it,which is exactly as Skovly and sharkTwo did.However,I don't know how to do this using C#,and the link gave me the answer.
Marshal.ReleaseComObject(titleShape); titleShape = null;
Just like this.

How to save an Excel file via VB 2010 without any dialogs (such as "save as")

I am trying to Save an Excel file Via VB 2010, and I have these questions
How can I disable the "save as" dialog? I tried things such as only "save" instead of "save as", but it doesen't work...
After I saved the file (using the save as) I can't Delete it... (I tried closing the excel file, Visual basic etc...) all i get is an error saying it is allready open in excel...
Is there a way to make VB show me the tips for writing the excel stuff (Ie - when I write messagebox. - it pops up "Show" for help. how can I enable this for excel code [worksheets.cells. ect.])
the connection:
Sub Connect()
' Connect to the excel file
oExcel = CreateObject("Excel.Application")
'Devine the workbook
oBook = oExcel.workbooks.open("e:\Words\Heb.xls")
End Sub
the saveas:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
oExcel.SaveAs(oExcel.Path & ".xls")
End Sub
Thanks a lot
I think Inafiziger has solved your main issue, it should be a vanilla Save.
As it was unclear to me exactly what your are doind (ie Visual Studio/VB/BA) then
On (1)
I thought it worth clarifying that you can use code inside the ThisWorkbook module to detect and handle a SaveAs if you are providing users with a choice. This Event detects the SaveAs and cancels it
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
MsgBox "You cannot use SaveAs to save this file", , "Save Cancelled!"
Cancel = True
End If
End Sub
This code can be programmatically added to your target workbook but I doubt you would need to resort to this given you should be able to run the simple Save.
On (3)
You need to use Early Binding to get the benefit of intellisense. You are currently use late binding with oExcel = CreateObject("Excel.Application"). A commonly used approach is to write the code and get it working with early binding, then converting it to late binding for final code publication.
Conditional Compilation (see comment at bottom) can be used to switch between the two binding methods within the same code.
You should be saving the workbook. e.g. oBook.Save.
If you create a new file, you will need to use SaveAs with a valid filename in order to save it the first time.

Resources