Finding Image reference in MS Word using VBA - excel

I am trying to copy an excel Table and image from MS EXCEL to MS WORD using VBA. I was struggling to find out how will I reference the tables and images into the Word once they are sent from excel.
After a long research I came across a very simple answer for table :
Range("C1:D8").Copy
Dim WDDoc As Word.Document
Dim table1 As Word.Table
Dim para As Paragraph
Set para = WDDoc.Paragraphs.Add
para.Range.PasteSpecial Link:=False, DataType:=wdPasteRTF,
Placement:=wdInLine,
DisplayAsIcon:=False
set table1 = WDDoc.Tables(1) ' getting reference for the Pasted tables in word
table1.Shading.BackgroundPatternColor = wdColorBlueGray
What I have done:
I am able to copy an image from excel and simply pasting it on to word doc. para.
What I need?
after copying n picture i am not able to refer to that picture in word and hence not able to edit or resize the image once it is pasted.

Try this
With ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count)
.Height = 314.95 ' or whatever
End With
This assumes that the picture is pasted "in line with text" (as you do) and is the last (furthest down) in-line picture in the document.

Related

Copy Word content into Excel spreadsheet using Excel VBA

I have a set of word documents which contains evaluation forms. I can manually copy and paste these along with their formatting into an excel spreadsheet, but I am interested in automating this using VBA since I have about 400 of these.
How can I open each of these and copy and paste the data into excel while retaining all of the formatting?
I would get the text from clipboard with:
Dim DataObj As New MSForms.DataObject
DataObj.GetFromClipboard
myString = DataObj.GetText
and then parse that text. You can check out this link https://excelmacromastery.com/vba-string-functions/#Extracting_Part_of_a_String
The first stage is to set a reference to Microsoft Word in the vb editor in Excel. You can then open a word document like this
Dim wd as new Word.application
dim doc as word.document
set doc = wd.documents.open("path and mame of word document")
'working with a table is like this 'Assume target is a pointer to an excel cell
Dim t As Word.Table
Set t = doc.Tables(1)
t.Cell(3, 2).Range.Copy 'this copies the cell at row 3, column 2
target.PasteSpecial xlPasteValues
That should get you started

How do I create dynamic hyperlinks in powerpoint?

I am trying to link a powerpoint presentation to data in excel. However the excel and powerpoint will change locations every day.
Here is my methodology so far:
- Copy from excel
- Paste special... paste link... As: Microsoft Excel Worksheet (code) object
This allows me to live edit the excel document and the powerpoint updates in real time.
However, when I change the location of the excel document, all 100+ links are broken.
How do I dynamically set the path of the links so they always follow the correct excel document, no matter where the excel document is? The excel document will never name change, neither will the powerpoint.
I have been looking for a solution for over 6 months...
Thank you.
I've got a page on my PPTFAQ site that includes code that might help:
Batch Search and Replace for Hyperlinks, OLE links, movie links and sound links
http://www.pptfaq.com/FAQ00773_Batch_Search_and_Replace_for_Hyperlinks-_OLE_links-_movie_links_and_sound_links.htm
Here's the specific code ... it deals with both hyperlinks and OLE links (ie, your Excel links). You can remove the hyperlink stuff if you don't need it and pass the new path as a parameter rather than getting it from an InputBox:
Sub HyperLinkSearchReplace()
Dim oSl As Slide
Dim oHl As Hyperlink
Dim sSearchFor As String
Dim sReplaceWith As String
Dim oSh As Shape
sSearchFor = InputBox("What text should I search for?", "Search for ...")
If sSearchFor = "" Then
Exit Sub
End If
sReplaceWith = InputBox("What text should I replace" & vbCrLf _
& sSearchFor & vbCrLf _
& "with?", "Replace with ...")
If sReplaceWith = "" Then
Exit Sub
End If
On Error Resume Next
For Each oSl In ActivePresentation.Slides
For Each oHl In oSl.Hyperlinks
oHl.Address = Replace(oHl.Address, sSearchFor, sReplaceWith)
oHl.SubAddress = Replace(oHl.SubAddress, sSearchFor, sReplaceWith)
Next ' hyperlink
' and thanks to several astute user suggestions, let's fix OLE links
' and movie/sound linkes too
For Each oSh In oSl.Shapes
If oSh.Type = msoLinkedOLEObject _
Or oSh.Type = msoMedia Then
oSh.LinkFormat.SourceFullName = _
Replace(oSh.LinkFormat.SourceFullName, _
sSearchFor, sReplaceWith)
End If
Next
Next ' slide
End Sub
You can link data from a saved Excel spreadsheet or copy cells from an Excel spreadsheet into your Microsoft PowerPoint 2010 presentation.
https://support.office.com/en-us/article/import-data-from-excel-into-powerpoint-3ec295e9-1bfd-47ff-8d7d-8b838caef853
Use the "Name manager" function in "Formula" and define the name of the table/certain cells you want to paste on PowerPoint.
So that when you paste "Excel Object" on PPT, the link will not simply be the location of the table in a worksheet (something like R1C1), but the name you defined and will not be affected if you move the table around.

Paste table picture from excel to word to specific paragraph and page

The following excel vba code paste table as picture from excel to word file at the start location one after another as may times the code runs. Please some body help me with the code to past the table as picture in paragraph 9 and when it run second time and so on should able to paste in second page of paragraph 9 and so on.
Set objDoc = objWord.activedocument
Set rng = objWord.Selection
wb.Windows(1).View = xlNormalView
wb.Worksheets(1).Range(Worksheets(1).UsedRange.Address).CopyPicture Appearance:=xlScreen, Format:=xlPicture
rng.Paste
rng.typeparagraph
I have a entire word file with excel inputs, but i made it the other way arround...
On the world you can reference the picture from de excel file and the text you need, every time you open the word file it will update those inputs.

VBA Copy and Paste Table as Picture from Excel to Word using Content Controls

The following VBA code copies from excel a named range for a table as a picture and paste it into a word document as a picture using content controls. The content control name is the same as the named range in excel. The following snippet of code works.
Is there a way to make this code faster or more efficient?
Set tTable = Range(CCtrl.Title)
tTable.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set wdbmRange = wdDoc.ContentControls(CCtrl.ID)
If Occ.Type = wdContentControlPicture Then
If Occ.Range.InlineShapes.Count > 0 Then Occ.Range.InlineShapes(1).Delete
wdDoc.ContentControls(CCtrl.ID).Range.Paste

Change Link Excel sheet - graph object Powerpoint VBA

I am facing troubles with VBA coding.
I have an excel file with various sheets with data and graphs. These graphs are linked to a Powerpoint (graphs have been copied and paste "with link" as objects).
The issue, is that I now have a huge Powerpoint of more than 130 slides with about 18 graphs on each slide... So more than 2000 graphs.
I would like to change the name of my sheets and also to duplicate some slides to populate the graphs with filtered data.
My issue:
- If changing the sheet name, of course the link is broken. Updating everything by hand with the UI is just impossible;
- When duplicating a slide in PowerPoint, the graphs are still linked to the same Excel sheet as the original slide - the only way to change the link is to delete all graphs, duplicate the sheet in Excel - populating with new data - copying-pasting with link again each graph one by one into PowerPoint.
I have tried to use a macro but... it changes the whole address of the link, deleting all sheets information. Is there a way to modifiy the hard address but keeping the same excel file - only changing the sheet?
Here is what I am trying to use to replace the sheet "T3" by the sheet "100s". The macro runs without error but then all the objects are replaced by a copy of the WHOLE "100s" worksheet from my excel file :(
Sub EditPowerPointLinks()
Dim oldFilePath As String
Dim newFilePath As String
Dim pptPresentation As Presentation
Dim pptSlide As Slide
Dim pptShape As Shape
'The old file path as a string (the text to be replaced)
oldFilePath = "\\Server\01xxxx\xxx\xx\X 4.xlsx!T3"
'The new file path as a string (the text to replace with)
newFilePath = "\\Server\01xxxx\xxx\xx\X 4.xlsx!100s"
'Set the variable to the PowerPoint Presentation
Set pptPresentation = ActivePresentation
'Loop through each slide in the presentation
For Each pptSlide In pptPresentation.Slides
'Loop through each shape in each slide
For Each pptShape In pptSlide.Shapes
'Find out if the shape is a linked object or a linked picture
If pptShape.Type = msoLinkedPicture Or pptShape.Type _
= msoLinkedOLEObject Then
'Use Replace to change the oldFilePath to the newFilePath
pptShape.LinkFormat.SourceFullName = Replace(LCase _
(pptShape.LinkFormat.SourceFullName), LCase(oldFilePath), newFilePath)
End If
Next
Next
'Update the links
pptPresentation.UpdateLinks
End Sub
Would anyone have an idea on how to change only the sheet name and keeping all the object names after?
Thanks a lot,
Arthur
In fact, the formula works fine. The replacement of link didn't work because in the original sheet that I copied, the first object in the selection pane was Graph 3. When copying the sheet, Excel automatically tries to make it start at 1 so Graph 3 became Graph 1. Then, when replacing the links, the graphs didn't match.
To make this formula work, make sure in the Selection Pane in Excel that your graphs are named the same way between the original sheet and the new one.

Resources