Replacing text in an email with a picture from the clipboard - excel

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

Related

Copying Editable Excel chart to Word Error 4605

I have a macro that is intended to copy a chart from excel into a word document in the same way as manually copy and pasting using "Keep Source Formatting and Embed Workbook." Below is the code that, to my understanding, should accomplish this.
Set reductionChart = graphWorksheet.ChartObjects("reduction")
reductionChart.Copy
masterReport.Paragraphs.Last.Range.PasteAndFormat wdChart
graphWorksheet is the worksheet that contains the graphs, masterReport is the word document.
The issue I am having is error 4605 command not available on the PasteAndFormat line. I happened to discover that manually copying the graph then running the line worked without issue. Thinking that maybe right clicking copied in a different way than .copy so I record a macro of the action and ended up with:
ActiveSheet.ChartObjects("Reduction").Activate
ActiveChart.ChartArea.Copy
Even substituting this in the error still occurs. What is happening here?
After some additional testing I am thinking that possibly when using .copy the chart is sort of only stored within excel and not the clipboard so when paste and format looks for something it see an empty clipboard and has an error, but right clicking copy stores it to the clipboard hence why is available still even after I run ActiveChart.ChartArea.Copy again.
I've attempted to create new workbook with a single sheet and chart. Also tried using late binding instead on the off chance that did something. This is the full code still giving the same issue
Sub test()
Dim masterWord As Object
Dim masterReport As Object
Set masterWord = CreateObject("Word.Application")
Set masterReport = masterWord.Documents.Add
masterWord.Visible = True
ThisWorkbook.Worksheets(1).ChartObjects("Chart 1").Copy
masterReport.Paragraphs.last.Range.PasteAndFormat wdChart
End Sub
After much testing this is the best workaround I'm managed to figure out.
reductionChart.Chart.ChartArea.Copy
masterReport.Paragraphs.Last.Range.PasteAndFormat wdFormatOriginalFormatting
masterReport.InlineShapes(masterReport.InlineShapes.Count).LinkFormat.BreakLink
wdFormatOriginalFormatting and wdPasteDefault both work and don't seem to make a difference to the outcome as far as I have seen.
This has one issue that I've found which is that on ActiveDocument.Fields.Update an information box will popup warning that the linked file is unavailable. This will occur for each unlinked item. I attempted to use Application.DisplayAlerts = False but this did not prevent the popup. This may come back to bite me, but I simply removed this line as it was unnecessary for my purposes.

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.

Image will not replace text in email body unless email is displayed, why?

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. Thanks to another question I have asked, I can replace the text properly, paste the image inline as I wanted and resize it but something strange has started happening.
When I run my macro as is, the text is not replaced and the image is not added. When I step through it and add a quick .Display line for troubleshooting, the image is displayed. I would like to avoid having to display the email before I send it just for efficiency purposes. I don't understand how or why this is happening.
Here is a quick sample of my code:
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookEmail = OutlookApp.CreateItemFromTemplate(templatePath)
ThisWorkbook.Worksheets("Email Sheet").Pictures("Summary Email Screenshot").Copy
Set EmailWordEditor = OutlookEmail.GetInspector.WordEditor
Set EmailText = EmailWordEditor.Content
With EmailText.Find 'Using the .Find method to find the text identifier
.ClearFormatting
.text = "*INSERT IMAGE HERE*"
.MatchCase = True
.MatchWildcards = False
If .Execute Then 'If found, delete text, paste image
EmailText.Delete
EmailText.PasteSpecial Placement:=wdInLine
End If
End With
EmailWordEditor.InlineShapes(EmailWordEditor.InlineShapes.count).ScaleHeight = 75
'OutlookEmail.Display 'With this uncommented, the image is loaded properly
OutlookEmail.Send
Set OutlookApp = Nothing 'Unload the objects properly
Set OutlookEmail = Nothing
I am pretty well versed in Excel VBA, but this is my first attempt at Outlook and Word VBA so please, bear with me.
EDIT 11/30/18
I have looked into the problem more and have learned that in doing a .Display, Excel evokes certain checks to validate the data in the email. From a comment on another post to stack overflow by Dmitry Streblechenko:
When you touch Outlook inspector (through Display, Close, GetInspector), Outlook runs extra code that validates your data. You got lucky it was smart enough to fix your attachment. If you do not want to rely on that, set the PR_ATTACH_CONTENT_ID property explicitly - that is how Outlook knows which attachment must be used for a particular img tag.
I do not want to touch the email body with HTML as there is content in the template that changes on a weekly basis before the *INSERT IMAGE HERE* identifier. If I was building the email from scratch, the HTML route would probably be easier, but I chose the .WordEditor route to specifically circumvent this issue. I also do not know how to code in HTML at all, so it would be more difficult for me to try to solve this problem with it. Is there any way I can manually validate the content of the email body without having to use .Display to make Outlook do it?

Paste/Copy Range of Cells from Excel to a Bookmark in Word using WORD VBA

I am looking at inserting/pasting a range of text data (40 columns) from Excel into bookmarks in Word. Most of the answers are done using Excel VBA, which is so not practical for my use case as I will have the Word document open, add a button that would run this 'import data' macro. I actually already have a button in the doc that inserts images into bookmarks, so that's one more reason I don't want to do it via Excel VBA.
I know this is not great code, but for the lack of definite leads, I'm throwing it here and hope that this gives you an idea of what I'm trying to achieve:
Sub ImportData()
Workbooks.Open ("\Book2.xlsm")
ActiveWindow.WindowState = xlMinimized
ThisWorkbook.Activate
Windows("Book2.xlsm").Activate
Range("A1:AF1").Select
Selection.Copy
Documents("test.docm").Activate
Selection.GoTo What:=wdGoToBookmark, Name:="Overlay_1"
Selection.Paste
End Sub
PS: It would be great if I could sort of 'transpose' the 40 columns into rows as it is pasted in Word.
Here's an update to my code based off #Variatus 's advice:
Sub ImportData()
Dim wb As Workbooks
Dim ws As Worksheets
Dim objSheet As Object
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
wb.Open ("C:\Users\pc\Documents\Book2.xlsm")
Set objSheet = CreateObject("Excel.Application")
ActiveWindow.WindowState = xlMinimized
Set ws = Workbooks("Book2.xlsm").Sheets("Sheet1")
ws.Range("A1").Value.Copy
With objWord.ActiveDocument
.Bookmarks("Bookmark_1").Range.Text = ws.Range("A1").Value
End With
End Sub
I'm getting this error:
Runtime Error '91':
Object variable or With block variable not set.
Notice how I stuck with a single cell reference for now (A1). I'll just update my code as I learn along the way :)
When you click the button in your Word document you want the following sequence to be initiated.
Create an Excel application object. Make sure that a reference to Excel has been set (VBE > Tools > References) so that Excel's VBA objects are available.
Using the Excel application object, open the workbook. Create an object. Place the object in an invisible window.
Definitely forget about activating or selecting anything in either the workbook or your Word document. The latter is active and remains active from beginning to end. The bookmarks are points in your document you can reference and manipulate by name without selecting them. The Excel workbook is invisible. You can access any part of it using the Range object.
The data you want from your workbook are contained in Worksheets. Be sure to create an object for the worksheet you are about to draw data from.
Excel tables don't translate very well into Word tables. If you do want to go that way I suggest that you use VBA to create the table you want in Excel (transpose the data before you import them into Word). However, you may find it easier to first create the tables you want in Word and then just copy values from your Excel source into the word tables. That would involve taking one cell value at a time and placing it into one Word table cell. Transposing would be done by the algorithm you employ.
Close the workbook. Quit the Excel application. Set the Excel application = Nothing. At the end of your macro everything is as it was before except that your document has data in it which it didn't have before.
Each of the above six points will lead you to at least one question which you can ask here after you have googled the subject and written some code. In fact, I strongly urge you to create one Main procedure (the one which responds to your button click) and let that procedure call various subs which carry out the individual tasks and functions to support the subs. The smaller the parts you create the easier it is to write the code, to find questions to ask and get answers to them. If you plan your project well expect to have about 12 procedures in it by the time you are done. Good luck!

Update embedded excel file programmatically

I'm trying to modify an embedded excel table in a word document programmatically. To do this, I have modified the docx file and the embedded excel file.
The significant part of the main document is the following:
<w:object w:dxaOrig="8406" w:dyaOrig="2056">
<v:shape id="_x0000_i1028" type="#_x0000_t75"
style="width:390.75pt;height:95.25pt" o:ole=""><v:imagedata r:id="rId14"
o:title=""/>
</v:shape>
<o:OLEObject Type="Embed" ProgID="Excel.Sheet.12" ShapeID="_x0000_i1028"
DrawAspect="Content" ObjectID="_1349794876" r:id="rId15"
UpdateMode="Always"/>
</w:object>
The word document uses an OLEObject to link to the embedded excel document. For displaying purposes,
a .wmf file is used (using the v:shape element). I have modified the excel document, which outdated this preview.
This results in some strange behaviour in the document:
The preview of the embedded (excel) table shows the wrong data
Double clicking on the embedded table opens the table in an embedded excel and shows the correct data
Closing the embedded editor triggers the generation of a new preview, showing the correct data
Of course, I want the table to show the correct table when the document is opened. How can I trigger Word to discard the image and redraw the preview?
An ideal solution for me would be to trigger the regeneration of the preview just by modifying the contents of the docx, but solutions using a small script would also help.
There is no perfect solution to this, but one that works most of the time is to force an open/close of the OLEFormat.Object. It doesn't matter if you are rehydrating the embedded Excel worksheet from outside of Word (i.e. manipulating the Open XML format) or doing it through the object model. What it involves is opening the embedded Excel spreadsheet from within Word and then closing that object for the image to be changed to the current values in the embedded spreadsheet and the new image to be created.
It depends just a bit if you are doing this on many Word documents or just one. In the former case, a global template (such as normal.dot or a custom one you create and put in the STARTUP folder) or in the later case, just run code behind of one document. Both have a different way to getting things to run, but essentially you will be hooking the Document_Open event and from there checking if the current document has OLE Embedded objects and if so, opening and closing them.
Without going into the hook, like I said, the code isn't pretty. Basically because it uses SendKeys. Most of the time, this will work. Some of the time, it won't. That is the nature of SendKeys and other programs receiving the focus without consent (such as an instant messenger program).
If Word has the focus (which means you can't open the VBE and press F5), this code should do the trick:
Sub UpdateEmbeddedXLSX()
Dim workbook As InlineShape
For Each workbook In ActiveDocument.InlineShapes
With workbook
If .Type = wdInlineShapeEmbeddedOLEObject Then
''# Excel.Sheet.12 for Excel 2007
If .OLEFormat.ClassType = "Excel.Sheet.12" Then
''# Open Object as spreadsheet
.OLEFormat.DoVerb wdOLEVerbPrimary
''# If you want, you can also do any updates here like
.OLEFormat.Object.ActiveSheet.Cells(2, 2).Value = ".1"
''# Nasty - but it works - SendKeys
SendKeys "{ESC}", True
End If
End If
End With
Next
End Sub
At the very least, you could put this code in your normal.dot and assign it to the QAT to be run as a macro.
Note that the code doesn't get around Excel opening, the values changing and then closing - that is part and parcel of using embedded objects. Using linking instead of embedding would be a much smoother way to do all of this, but I realize it's not always an option.
Just to add to an old post in the event someone stumbles upon this like I did:
The above code works great, but I modified it to use bookmarks instead of using SendKeys. The SendKeys statement really messes with the NumLock on my keyboard. Just one of the quirks of that command.
What I did was create bookmarks in my Word Doc Template. Then in my code, I created a pointer to the bookmark:
Dim bMark as bookmark
Set bMark as ActiveDocument.Bookmarks("NameOfBookmark")
Then in place of the SendKeys statement, I did the following:
bMark.Range.Select
Selection.EndKey
This basically pulled the focus out of the embedded worksheet, and onto the bookmark of the page. Then the .EndKey statement simply removed the selection. You don't really even need it.
Hope this helps!
If anyone's coming here looking for how to update an Excel Object embedded in a Visio document via VBA macros without double-clicking, you just need to call Save on the embedded workbook.
Dim wb As Excel.Workbook
Set wb = ThisDocument.Pages("MyPageName").Shapes("NameOfWorksheetShape").Object
'Calling me updates the visible worksheets.
wb.Save

Resources