I posted a question a few days ago regarding taking a screenshot of an excel worksheet's data area and then embedding that screenshot into the body of an email message composed in MS Outlook 2010. The expert who assisted successfully answered my question and the code works great!
My question now is it possible to make it dynamic meaning if the data area size changes is it possible to have to code auto adjust to the new data range? Right now its set for the current size A1:P40 in this part of the code.
Private Sub cmdEmail_Click()Dim oRange As RangeDim oCht As ChartDim oImg As PictureSet oRange = Range("A1:P40")Set oCht = Charts.AddoRange.CopyPicture xlScreen, xlPictureoCht.PasteoCht.Export Filename:="C:\Temp\SavedRange.jpg", Filtername:="JPG"Screenshot_Mail "Sample Email Address" & "; " & "Sample Email Address", "Sample Email Address" & _ "; " & "Sample Email Address" & "; " & "Sample Email Address", "Rep II Case Productivity Report", "<font color=red>" & _"<I>" & "Below is a Snapshot View of the Rep II Case Productivity Report: " & "</font>" & "</I>" & _"<BR>" & "<BR>" & "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _"<IMG alt='' hspace=0 src='C:\Temp\SavedRange.jpg' align=baseline border=0> </BODY>"DoEventsoCht.DeleteEnd Sub
If you create your charts dynamically, you can save the range you used, and pass it to the email function.
"Abys" Wallace
ASKER
@shorvath, if the range of data above the chart change in size due to increased rows and the chart gets shifted downward would the "set range" in the code capture all of the data? I may not be grasping your recommendation. If I save the range used and the data later increases or decreases will the screenshot portion of the code capture the new range? The chart properties are set not to "move or change in size".
Thank you for your help! :)
shorvath
abys757
I understand that the data in your grid is dynamic and can have any number of rows and columns. What I am trying to say, is that at some point your macro that inserts the data must keep track of how many rows are on the sheet, because you have to set the Charts Data Range. Your current Data Range is ='Printable_Version'!$A$1:$P$14
Now we can reposition the Chart based on finding the first blank row on the sheet using the following code. You can test this by shifting the chart down and to the right of it's current position. The code will detect the first blank row, shift the chart and reset the range.
replace your cmdEmail_Click code with this:
Private Sub cmdEmail_Click()Dim oRange As RangeDim oCht As ChartDim oImg As PictureDim iGetRows As IntegerDim strRange As StringiGetRows = Worksheets("Printable_Version").Cells.Find("*", _ Worksheets("Printable_Version").Cells(1), xlFormulas, _ xlWhole, xlByRows, xlPrevious).RowstrRange = "A1:P" & CStr(iGetRows + 25)Set oCht = Charts.AddDoEventsCall AdjustChartSet oRange = Range(strRange)oRange.CopyPicture xlScreen, xlPictureoCht.PasteoCht.Export Filename:="C:\Temp\SavedRange.jpg", Filtername:="JPG"Screenshot_Mail "Sample Email Address" & "; " & "Sample Email Address", "Sample Email Address" & _ "; " & "Sample Email Address" & "; " & "Sample Email Address", "Rep II Case Productivity Report", "<font color=red>" & _"<I>" & "Below is a Snapshot View of the Rep II Case Productivity Report: " & "</font>" & "</I>" & _"<BR>" & "<BR>" & "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _"<IMG alt='' hspace=0 src='C:\Temp\SavedRange.jpg' align=baseline border=0> </BODY>"DoEventsoCht.DeleteEnd SubPublic Sub AdjustChart()Dim oChtObj As ChartObjectDim oRng As RangeDim iGetRows As IntegerDim strRange As StringiGetRows = Worksheets("Printable_Version").Cells.Find("*", _ Worksheets("Printable_Version").Cells(1), xlFormulas, _ xlWhole, xlByRows, xlPrevious).RowstrRange = "A" & CStr(iGetRows + 2) & ":P" & CStr(iGetRows + 25)Worksheets("Printable_Version").ActivateActiveSheet.ChartObjects("Chart 3").ActivateSet oChtObj = ActiveChart.ParentSet oRng = ActiveSheet.Range(strRange)oChtObj.Left = oRng.LeftoChtObj.Width = oRng.WidthoChtObj.Top = oRng.TopoChtObj.Height = oRng.HeightSet oChtObj = NothingSet oRng = NothingEnd Sub
Your help has saved me hundreds of hours of internet surfing.
fblack61
"Abys" Wallace
ASKER
Hi shorvath.. sorry about the delayed response but I've been away from computer access. :)
I tried the revised code but for some reason it jumbles the row data with the chart image.
Would you recommend I move the chart above the data range? This way the chart image can stay static according to its properties size settings and the rows in the data range can increase or decrease in number. I have novice abilities at best with vb so let me know if I sound confusing. Again thank you for your time.
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
shorvath: Your workbook sample did work just fine. Not sure why mine was slightly merged. I appreciate your assistance!
"Abys" Wallace
ASKER
shorvath: Don't mean to be bother but I have a quick question ... I've come to a cross road where I'm requested to have the chart above the data area afterall ... Is there a simple way to mod your given code to accomodate? If needed I'll open an additional question to ensure proper point compensation.