Excel macro - keep source formatting - excel

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?

Related

Excel to Powerpoint Table macro

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

Copy value of variable in Excel 2016 macro to new doc in Word 2016

I want to insert in a W10 Excel 2016 macro some code that will
open a new Word 2016 document and
copy the value of variable "s" (string) from the Excel macro to that new Word document
then returns to the Excel macro.
Maybe it helps that I know Word can be opened with this macro:
Sub startWord()
Application.ActivateMicrosoftApp xlMicrosoftWord
End Sub
TIA for your help.
If it’s a simple copy of a string into a new Word document, then the following code should work. You haven’t indicated where you get the string from, so I’ve used the contents of cell A1 on Sheet1. You can adjust this to suit.
For the following to work, you need to enable a reference within VBA. In the VBA Editor, select Tools/References & check the box ‘Microsoft Word 16.0 Object Library’. If it’s not already checked, you’ll find it listed alphabetically.
This works for me – let me know how you go with it.
Option Explicit
Dim WordApp As Word.Application, myDoc As Word.Document, s As String
Sub CopyToWord()
On Error Resume Next
Set WordApp = GetObject(class:="Word.Application")
On Error GoTo 0
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
WordApp.Visible = True
WordApp.Activate
Set myDoc = WordApp.Documents.Add
s = Sheet1.Range("A1").Value
With myDoc.Content
.Text = s
End With
End Sub

Populate Excel File from Word Document Tables - VBA

I'm fairly new to VBA and trying to populate a preexisting excel document based on Word Documents.
The Word Documents will have three tables, and certain cells will become the Excel columns. The idea is, every day new product information sheets come in and the Excel sheet will need to be appended. I've started by looking over this previously asked question. Do I create a macro-enabled excel sheet and run it from within Excel? Could I get the macro to look inside a directory for the word documents, and perform an iterative macro?
How about this?
Sub ImportFromWord()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(ThisWorkbook.Path & "\My document.doc")
'Use below line if document is already open.
'Set wrdDoc = Documents("My document.doc")
With wrdDoc
N_Of_tbles = .Tables.Count
If N_Of_tbles = 0 Then
MsgBox "There are no tables in word document"
End If
Set wrdTbl = .Tables(1)
ColCount = wrdTbl.Columns.Count
RowCount = wrdTbl.Rows.Count
' Loop through each row of the table
For i = 1 To RowCount
'Loop through each column of that row
For j = 1 To ColCount
'This gives you the cell contents
Worksheets("sheet1").Cells(i, j) = wrdTbl.Cell(i, j).Range.Text
Next j
Next i
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
MsgBox "completed"
End Sub
That's the simplest solution. In Excel set a reference to Word in the VB Editor using Tools, References - you can then write code to manipulate Word from within Excel. You can use the keyword DIR to look for files in a folder, then declare a Word object, open the word document, iterate over the tables in the document and copy the values across to the right cells in Excel. Just watch for the ^p character that Word sticks in the cells - I tend to out the word cell's contents into a string variable and then take Left(s,len(s)-1) into excel to drop the last char.

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

Collating Excel Worksheets Imbedded in Powerpoint

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.

Resources