Creating an image from a userform of exported charts - excel

I have written a code that charts various data. The charts are then exported as .gif files. The image files are then loaded as pictures into image controls in a userform as shown below.
How do I go about exporting the whole userform (preferably without the 3 buttons on the right) as an image for use in a report?
Currently I am using manual printscreen but it is quite time consuming..
Chart Example
Currently I am manually using printscreen but it is quite time consuming..
Is there a way to access the alt-printscreen button perhaps?
The chart creation code itself isn't really relevant but I can share if required.
I expect an image file that I can then use within a word document.
Preferably cropped to remove the 3 buttons on the right of the userform.

Steven,
In the below sub, I used the SendKeys method to send Alt+PrintScreen ("%{1068}") and Ctrl+V ("^v") on the current application. The SendKeys method simulates the key presses of a keyboard, so it would replicate what you're doing right now.
Sub PrintTheScreen()
Application.SendKeys ("%{1068}")
DoEvents
Application.SendKeys ("^v")
DoEvents
End Sub
Reference for this method can be found here
Having the sendkeys applied to the userform will simulate your button press. By then, you could set a word application object and paste the printed screen, for example.
Sub CreatingWDDoc()
Dim appWD As Word.Application
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.Documents.Add
appWD.SendKeys ("^v")
' Change the string to the path you wish to use for seving the file
appWD.SaveAs (“”)
appWD.Quit
End Sub
Keep in mind that to use such functions, you need to have the word library referenced in your project, under Tools >> References. More information can be found here.

Related

Save the current graph in a userform chart by using a 'save as' function linked to a command button?

I have a userform that allows me to calculate values and draw graphs from these values. The graphs are stored in an Excel sheet during this step.
The userform contains a chart that allows me to display inside the graphs I have drawn.
A spin button allows me to navigate between the graphs in the chart. For example if the spin button value is equal to 1 and I have drawn 3 graphs in my Excel sheet, the first graph that has been drawn will be displayed in the userform chart.
Now what I'm trying to do is to create a macro linked to a command button that allows me to save the graph that is displayed in the chart (the current graph).
I want to use a 'save as' interface because the user has to choose where to save the graph.
I found the following code on a forum:
Sub filesave()
Dim bFileSaveAs As Boolean
bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
End Sub
By clicking my command button associated to this function, a "save as" window opens but allows me to save the only the Excel workbook.
So, I would like to adapt this code to my problem. If you have any ideas, I'm all ears.

Inserting and Linking a Picture into a Shape Fill using VBA

I am trying to link and insert a picture (*.png) into a shapes fill in powerpoint using vba in Excel. In the end I will loop through this on 100+ pages and the pictures will be updated frequently so automating this will be a huge time saver. Currently I have figured out how to loop through the pages and insert the pictures into the shapes, but I have been unable to figure out how to link the pictures too.
I'm using the below code to fill the shape with the picture but I can't find the syntax to both insert and link it:
Pres.Slides(1).Shapes(ShapeName).Fill.UserPicture PictureFilePath
Ultimately this should behave like clicking on a shape, going format > shape fill > picture > insert and link (On the drop down next to insert in the dialog box).
Not all user interface actions are in the VBA object model. One of those exceptions is creating a link to a shape fill. The closest you can get is to link pictures that are inserted as pictures rather than as fills. Here's the syntax to add a linked picture. It assumes the picture is in the same folder as the presentation:
Sub Macro1()
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="Picture1.png", LinkToFile:=msoTrue, SaveWithDocument:=msoFalse, Left:=300, Top:=251, Width:=121, Height:=38).Select
End Sub
Welcome to SO.
For answering your question, I'll give some credit to this similar SO question based on Excel and this similar SO question based on PP. Some additional information was gathered from the microsoft documentation on the subject.
What you seem to be looking for is the ActionSetting object in the shapes class, which seems to be availible to shapes in PowerPoint. Below is a code snippet created directly in PowerPoint VBA
Sub insertPictureIntoShapeWithLinkToPicture()
Dim PP_Slide As Slide, PP_Shape As Shape, imagePath As String
Set PP_Slide = ActivePresentation.Slides(1) 'Set slide (change as necessary)
Set PP_Shape = PP_Slide.Shapes(1) 'Set shape (change as necessary)
imagePath = "Path to image"
With PP_Shape
'add picutre
.Fill.UserPicture imagePath
'Set an action on click
With .ActionSettings(ppMouseClick)
'Set action to hyperlink
.Action = ppActionHyperlink
'Specify address
.Hyperlink.Address = imagePath
End With
End With
End Sub
The same approach should be available via Excel, either adding a reference to the PowerPoint object library, or using Late-Binding methods. Note that the 'click' method with hyperlink defaults to standard hyperlink clicking (ctrl + left mouse for windows with a Danish keyboard).
Please find attached code.
First create a shape in PPT and run the code.

Replacing text in an email with a picture from the clipboard

I have a report that I send out daily.
In it there is a linked image to a range that I need to paste into an email and send.
I have the email saved as an outlook template file that I modify as necessary when I need to.
The code I have runs from Excel.
I create an Outlook object and open the Outlook email from the .oft template file, then I want to find the identifier text (INSERT IMAGE HERE) and replace it with the linked image from Excel, which has been copied from the clipboard.
I am trying to add it as an InLineShape so that it behaves properly in the body of the email.
I have been attempting to do it with a defined WordEditor from the body of the email.
I looked into HTML to do it, but I decided the WordEditor object would be a better approach.
After I paste it, I need to set the scale height to 0.75 so that it is displayed properly.
From there, just click send.
Here is a quick sample of my code:
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookEmail = OutlookApp.CreateItemFromTemplate(templatePath)
OutlookEmail.Display 'Just for code checking
ThisWorkbook.Worksheets("Email Sheet").Pictures("Summary Email Screenshot").Copy
Set EmailText = OutlookEmail.GetInspector.WordEditor
With EmailText.Range.Find
.ClearFormatting
.Execute findText:="*INSERT IMAGE HERE*", MatchWholeWord:=True, MatchCase:=True, _
MatchWildcards:=False, ReplaceWith:="^c", Replace:=wdReplaceOne
End With
EmailText.InlineShapes(EmailText.InlineShapes.count).ScaleHeight = 75
'OutlookEmail.Send
The problem arises with that last line before I send it.
What happens is it is not inserted as an InLineShape, but just a shape so that it floats on top of the text.
I need it as an InLineShape so that the content after the image moves down and is displayed rather than being covered by it.
I've been scowering forums for a while to try to find an answer but to no avail.
I am pretty well versed in Excel VBA, but this is my first attempt at Outlook and Word VBA so please, bear with me.
By default when an image is pasted Word will use the setting Insert/paste pictures as from File/Options/Advanced, section "Cut, copy and paste". So on some machines an image might be pasted as an InlineShape and on others as a Shape.
If you were simply pasting in the code then using PasteSpecial it would be possible to specify inline or with text wrap using the corresponding WdOLEPlacement enumeration value of either wdFloatOverText or wdInLine.
Since the paste command is coming from the ^c replacement text this approach won't work for this situation. That means the Options setting will need to be changed. And, in order for this to be user-friendly, at the end of the code it should be changed back.
Here's some sample code demonstrating how to check the current option, change it if necessary, and reset at the end of the procedure. I'm not familiar with using Word through the Outlook object model, but I think I've correctly instantiated an object for the Word.Application so that VBA executing in Excel or Outlook will know what Options are meant. Except for the last line, this should come between Set and With in the code sample in the question.
Dim wrapType As Long
Dim wordApp as Object
Set wordApp = EMailText.Application
wrapType = wordApp.Options.PictureWrapType
If wrapType <> wdWrapMergeInline Then
wordApp.Options.PictureWrapType = wdWrapMergeInline
End If
'Do other things, then reset
wordApp.Options.PictureWrapType = wrapType

Store location of command button cell address to variable in VBA

Okay so I'm using Excel and trying to get a cell address based off the location of the command button I'm clicking. I want to store that cell address in a variable as I click the button to be used in the next code for the user form that pops up as a result of clicking that button. Ive looked at this link: Store location of cell address to variable in VBA , which is similar to what I'm trying to do but I cant seem to get it to work with the ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address that gives an address based off the top left corner of the command button. I'm trying to do it this way so I can use the same code from the next part for every similar button I add without having to manually change the code based off where the button is.
I'm pretty new when it comes to Excel and VBA so any help would be appreciated.
Edit:
I tried adding the code but I'm getting a Runtime error '1004' Unable to get Buttons property of the Worksheet class. I have this
Sub Button2_Click()
Dim BtRng As Range
Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
MsgBox Btnrng.Address
Btnrng.Offset(3, 3) = "Hello"
formAddBill.Show
End Sub
This button is also popping up a userform for information input. the error is happening on Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
If you are using application.caller then I assume your buttons are from the forms.control toolbox. This makes life a lot easier.
Assign each button with this code example:
Sub Rng_Butn_Clicked()
Dim BtRng As Range
Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
MsgBox Btnrng.Address
Btnrng.Offset(3, 3) = "Hello"
End Sub
If you are using command buttons from the activex toolbar, it can get a bit confusing. Doing it one at a time is easy enough
Private Sub CommandButton1_Click()
Me.CommandButton1.TopLeftCell.Offset(0, 1) = "Hi"
Me.CommandButton1.TopLeftCell.Offset(, 2).Interior.Color = vbBlue
MsgBox Me.CommandButton1.TopLeftCell.Address
End Sub
But assigning one code to many activex command buttons is not as easy, I would prefer to use the Forms Buttons if you want to assign one code to many buttons.
Edit: If you are getting this error
You probably put the code in a command button. Please reread my answer.
This is what I am referring to:Form Controls & ActiveX Controls

Have .jpg files within excel when using ActiveX Image Control

I have an excel file with several toggle buttons. When a toggle button is on and the "calculate" command button is pressed, my activeX image frame changes. However, I have all these files in the same directory so I'd have to send the file to my employees zipped. Is there anyway to like maybe load them into the excel workbook on a hidden sheet?
Thank you
I think the LoadPicture() function will look for a system file so you don't want that.
Add the pictures into images on a worksheet. This way they will be in the workbook.
Then right click the image you just added and select properties. Select picture property and navigate to your image file.
You can change the name to make sense of your pictures also. So they don't have to be Image1 Image2 etc.
Then in your code set the picture that you want to change = the image that you want.
If something
Image1.Picture = Image2.Picture
Else
Image1.Picture = Image3.Picture
End If
Here Image1 is the picture that changes based on what happens when the calculate button is pressed. Image3 is one of the images that you loaded into the workbook.
If you are going to store them on some other worksheet you may need to declare a worksheet object and set it to that sheet
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("ImageWorksheet")
Image1.Picture = ws.Image3.Picture
or something like this may work.
ActiveWorkbook.Sheets("ImageWorksheet").Image3.Picture
Something like that.

Resources