Copy / Paste from other workbooks - image

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

Related

Active IE 11 window for SendKeys

Problem : I am trying to automate the saving of a file (manage to save the file when the below IE bar appears at the bottom of the page)
My code is going on my intranet, clicking here and there and then I click on an "Export" button which will trigger this from IE :
I didn't manage to find a way to automate the saving of the file because the only way (I think) to interact with this "window" is to use SendKeys.
In order to do so I have to "activate" this window (Yes, I have it activated for the HTML scraping with this bit of code, but it's not the active window though):
I tried using AppActivate but for some reason it doesn't work.
There are 2 options to pass this obstacle :
Find a way to activate the IE window containing that saving bar so that I can use Application.SendKeys "%{S}" on it
(second option only) : Disable this saving bar or manage to have it open in a new window
So far I have gone through lots of posts talking about that subject but none gave an operational solution for that issue on IE 11.
Let me know if you want to see any bit of code, I have a lot of different attempts gathered from different posts but this would highly increase the length of this post.
Front-end automation is a tricky business. It can be really hard to find a solution that always works (those pesky users are free to move the mouse and click buttons at will, disrupting your code). Because you are taking the data from an intranet site, this suggests the data you need already exists within your organisation. If at all possible (and I know it isn't always) take the data from the servers/source systems, rather than via the UI.
AppActivate and SendKeys can be very fussy. Because the url is always the same a better approach would be to directly download it. Here is a example, based on another answer.
Before you can run code you will need to add two references:
Microsoft XML, v6.0
Microsoft ActiveX Data Objects 2.8 Library
From the VBA IDE click Tools >> References... and select from the list (the original answer does not use references, instead it uses late binding).
' Downloads a file from a given URL.
' URL String URL to get file. Ex: http:\\MySite\MyFile.
' Path String Location to save file. Ex: C:\Myfile.csv.
' Requires: Microsoft XML, v6.0
' Requires: Microsoft ActiveX Data Objects 2.8 Library.
Sub DownloadURL(ByVal URL As String, ByVal Path As String)
Dim httpXML As XMLHTTP60 ' Used to download data.
Dim st As ADODB.Stream ' Used to write downloaded data to file system.
Set httpXML = New XMLHTTP60
' Request file.
httpXML.Open "GET", URL, False
httpXML.send
' Download response.
If httpXML.Status = 200 And httpXML.readyState = 4 Then
Set st = New ADODB.Stream
st.Open
st.Type = adTypeBinary
st.Write httpXML.responseBody
st.SaveToFile Path, adSaveCreateOverWrite
st.Close
Else
MsgBox "Something went wrong :(", vbCritical, "An error occured"
End If
End Sub

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

Copying multiple images from one workbook to another as separate images

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

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.

Excel - Open Multiple Windows and Tile on Sheet Tab Click

I'm trying to write a macro and I have the 2nd half done (tile windows showing specific sheets) but not sure how to do the first half.
There are some sheets that when displayed I would like to look at two other sheets at the same time (multiple window tiling). This is easy to set up manually, but I'd like it to happen automatically when I click on any of the 3 sheet tabs that are in the spreadsheet. There are other sheets that should be handled normally and have only 1 window open.
I'm thinking that I need to set up some sort of onClick event, but I haven't ran into it before in Excel and didn't see anything initially in Google.
Google figured it out once I rephrased my question. In VBA, select the Sheet object (not a module) and use this code:
Private Sub Worksheet_Activate()
'Your code goes here
End Sub
The code will be executed when the sheet is activated.
My reference: http://www.mrexcel.com/forum/showthread.php?t=71663

Resources