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
Related
I've been trying to build an Excel macro that can copy a range of numbers and paste that range into a Powerpoint table. I have the code below which appears to work super well but was hoping to add two tweaks to it//get help on how I can add the following functionality:
When it copies over to Powerpoint, it inputs the numbers with a bullet point before each number. Ideally, I'd love for it to paste the numbers over without any additional formatting.
Is there a way to copy the Excel coloring from Excel to the Powerpoint table? Right now, it just pastes the raw data without any formatting. But ideally, since a lot of the times I have colored conditional formatting on the Excel cells, this macro could copy each cells coloring and paste it into the Powerpoint table. (I was helped with some of the ExecuteMSO functions at the bottom of the code, but when I uncomment it/try to run it I get an error)
Right now, it requires the manual change of the Excel ranges to capture all the wanted data ("Range A3:J"). Is there a way to have Excel just copy the data within the highlighted/selected range so it can be flexible?
Really appreciate any help with this! (pasted the current code iteration below)
Public Sub Excel_to_PPT_Table()
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppTbl As PowerPoint.Shape
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
If ppApp Is Nothing Then
Set ppApp = New PowerPoint.Application
Set ppPres = ppApp.Presentations.Item(1)
Else
Set ppPres = ppApp.Presentations.Item(1)
End If
ppApp.ActivePresentation.Slides(1).Select
ppPres.Windows(1).Activate
' find on Slide Number 1 which object ID is of Table type (you can change to whatever slide number you have your table)
With ppApp.ActivePresentation.Slides(1).Shapes
For i = 1 To .Count
If .Item(i).HasTable Then
ShapeNum = i
End If
Next
End With
' assign Slide Table object
Set ppTbl = ppApp.ActivePresentation.Slides(1).Shapes(ShapeNum)
' copy range from Excel sheet
iLastRowReport = Range("B" & Rows.Count).End(xlUp).Row
Range("A3:J" & iLastRowReport).Copy
' select the Table cell you want to copy to >> modify according to the cell you want to use as the first Cell
ppTbl.Table.Cell(3, 1).Shape.Select
' paste into existing PowerPoint table - use this line if you want to use the PowerPoint table format
ppApp.CommandBars.ExecuteMso ("PasteExcelTableDestinationTableStyle")
' paste into existing PowerPoint table - use this line if you want to use the Excel Range format
' ppApp.CommandBars.ExecuteMso ("PasteExcelChartSourceFormatting")
End Sub
I have an Excel worksheet with words in each cell. I wanted to write this row
onto a Word document.
The macro worked pefectly the first time.
When the macro is run, the docx file has already been created but not opened.
I rubbed out the text in the word document and closed it
Then I ran the Excel macro again and got a message saying that the document
was locked for editing by myself.
There was an option, then, to create a local copy. I did this and eventually
I got the text on to the Word document.
What is going on, please ?
I simply don't understand what this "locked for editing" is.
I think the relevant lines of code are :
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
Dim oRng As Word.Range
destfilename = "E:\......\Dummy_Script.docx"
Set objDoc = objWord.Documents.Open(destfilename)
'
' TextA is a string with the text of the row
'
Set oRng = ActiveDocument.Range(Start:=0, End:=0)
oRng.Text = TextA
"Code"
Background: I am on the backend of an effort to capture and collate data collected in PowerPoint template form. A template was distributed. The result is ~150 PowerPoint 2010 presentations of ~30 slides each. ~15 of slides in each presentation contain an imbedded XLS.
Benefit to community: Examples of PowerPoint to Excel techniques and in general MS Office techniques vs. solely one MS Office tool.
Problem: I'm only an introductory VBA developer. I seem to find many examples how to get Excel data to PowerPoint, but not much (!) about this seemingly backward approach of data from PowerPoint into Excel. PeltierTech.com gets me close. I found some texts but need a solution before I can get through them.
Need:
1) Loop through all presentations (.PPTX) in a folder (open/close)
2) Inspect each slide in each presentation for an imbedded XLS
3) If found
a) Copy the imbedded source XLS range (not image)
b) Find the last row of the target XLS tab
c) Write the .PPTX name into tab column A
d) Paste the source XLS into target Excel column B
Finally I would prefer the "host" VBA be Excel.
The ideal result is a single .XLSX with ~15 tabs. The resultant data can be scrubbed for unique headers and converted into a pivotable dataset.
This doesn't appear the most to be the most challenging exercise. I think I'm hung on combining the two object models in a single set of procedures. (Yes, the references are correctly set ;-) )
THANK YOU!!!
Here is an example I wrote to extract one chart data from PP file (code is in a excel module). I've commented the code so you will be able to build your own loops to this, but the mechanics of extracting chart data from PP-file via Excel-VBA is as in the following block -
'"Microsoft PowerPoint xx.x Object Library" needed to be installed
'
Sub OpenPPandCopyChartData()
'Create an instance of Powerpoint
Dim PPT As Object
Set PPT = CreateObject(Class:="PowerPoint.Application")
'Open the powerpoint file
Dim pp As PowerPoint.Presentation
Set pp = PPT.Presentations.Open(Filename:=ThisWorkbook.Path & "\Presentation1.pptx", ReadOnly:=msoTrue)
'Select the wanted slide
Dim ps As PowerPoint.slide
Set ps = pp.Slides(1)
'Select the shape
Dim sh As PowerPoint.Shape
Set sh = ps.Shapes(2)
'You can make your own loop to check all the shapes if they contain a chart by using HasChart Method
Debug.Print sh.HasChart = msoTrue
'Select the shape that has chart and activate
sh.Chart.ChartData.Activate
'Set the activated workbook to a variable
Dim wb As Workbook
Set wb = sh.Chart.ChartData.Workbook
'Select the sheet the data is contained
Dim ws As Worksheet
Set ws = wb.Sheets(1)
'Select the range and copy
ws.UsedRange.Copy
'Set where to copy
ThisWorkbook.Sheets(1).Range("A1").PasteSpecial xlPasteValues
wb.Close
pp.Close
PPT.Quit
End Sub
The code opens the PPT.file in the same folder that the Excel file is in and selects Slide(1) and Shape(2) from that slide. Then it activates the chart data (if not present this code will cause a subscript out of range error, so on your own code a data checking will be needed) and copies it to the Excel that the sub was started in.
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.
When I try to copy data between worksheets this is no problem, but when I try to copy the same data to a word document it loses its format. Is there a way to stop this?
' Copy all data from 1.xls to new.docx
Sheets("Design").Select
Range("A1:G50").Copy
appWD.Selection.Paste
Could it be something with PasteSpecial?
Thanks.
#Brown
Select Case Range("C19").Value
Case 1
Sheets("Info").Select
Range("B7").Copy Destination:=Sheets("Design").Range("A" & x)
x = x + 2
End Select
So this copies the data from cell C19(Sheet: Info) to cell B7(Sheet: Design)
' I open my word doc etc.
Sheets("Design").Select
Range("A1:E50").Copy
appWD.Selection.Paste
This selects sheet Design, copies everything and pastes this into a word doc. I lose my formatting, I'm also using XP, office 2007.
Here is my simple test program
Sub test()
Dim wrdApp As Word.Application
Set wrdApp = New Word.Application
Dim wrdDoc As Word.Document
wrdApp.Documents.Add
Set wrdDoc = wrdApp.ActiveDocument
Range("A1:B1").Copy
wrdApp.Selection.Paste
wrdDoc.SaveAs "D:\tmp\myworddoc.doc"
End Sub
This works with Office XP (I don't have Office 2007 at hand). Cells A1:B1 contain formatted numbers. This one works fine on my machine, the created word doc contains a table with formatted numbers, too. Can you try it on yours to see if it works?