I am coding in VBA in Excel. I have an Excel sheet with information. I have two sheets titled: Finance and Invoice. I want to the Finance page to have cells which I would fill out - just by typing in the cells. And then I want the Invoice page to have specific cells which are auto-populated from the information in the Finance sheet.
Then, I want the cells on the Invoice page to be moved into a Word document. I am using a macro I've titled Macro2.
In the top part of the code, I am copying information from cells in the Finance sheet into the Invoice sheet. That works. But then I am trying to take the filled out information in the Finance sheet and move it into a Word doc. I looked for code online - but it isn't working for me. The cells that I want to move into a Word doc are B1 through D19 on the Invoice page. The name of my Excel file is FinanceMarketing.
Sub Macro2()
'
' Macro2 Macro
'
'Ask user for input
userinput = InputBox("Type Associated Letter corresponding to Desired Invoice Population:")
'Copy Name
Sheets("Finance").Range("B2").Copy Destination:=Sheets("Invoice").Range("D3")
'Copy Email
Sheets("Finance").Range("C2").Copy Destination:=Sheets("Invoice").Range("D4")
'Copy Adress
Sheets("Finance").Range("D2").Copy Destination:=Sheets("Invoice").Range("D5")
'Copy Date
Sheets("Finance").Range("E2").Copy Destination:=Sheets("Invoice").Range("B8")
'Copy Amount Owed
Sheets("Finance").Range("I2").Copy Destination:=Sheets("Invoice").Range("D8")
'From here down in the part of the code that I found online to move the excel into word which isn't working
'Using Early Binding
Dim wordApp As Word.Application
Dim mydoc As Word.Document
'Creating a new instance of word only if there no other instances
Set wordApp = New Word.Application
'Making word App Visible
wordApp.Visible = True
'Creating a new document
Set mydoc = wordApp.Documents.Add()
'copying the content from excel sheet
FinanceMarketing.Worksheets("Invoice").Range("B1:D19").Copy
'Pasting on the document
mydoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
'saving the document
mydoc.SaveAs2 "MyDoc"
'closing the document
'mydoc.Close
'Emptying the Clipboard
'CutCopyMode = False
End Sub
Help to get moving the info into word would be very appreciated!
Related
I am trying to create a word document from an Excel sheet.
For instance, in my excel table, I have different columns. One of them has the title of "colour". I can FILTER this column by the name of the different colours (eg "blue" , "green", "yellow" etc.).
Let's say I filter my column with the colour "blue".
What I would like to do in a VBA script, is to select all these rows that have the colour "blue" in commun and copy-paste them in a word document.
I thought I had it, but with the script I am using, I do not know why, but only the titles of my different columns is pasted, regardless what I define.
Sub TestOne()
Dim appWD As Word.Application, wbXL As Excel.Workbook
Set appWD = CreateObject("Word.Application.16")
appWD.Visible = True
debut = Range("A13").End(xlUp).Row
fin = Range("A15").End(xlUp).Row
For i = debut To fin
'Copy the current row
Worksheets("Sheet1").Rows(i).Copy
'tell word to create new document.
appWD.Documents.Add
'Tell Word to paste the contents of the clipboard into the new document.
appWD.Selection.Paste
Next i
'Close the new Word document.
appWD.ActiveDocument.Close
'Save the new document with a sequential file name.
appWD.ActiveDocument.SaveAs Filename:="File"
' Close the new Word application.
appWD.Quit
Set appWD = Nothing
End Sub
If anyone has already done such a thing, or knows how to fix this script, I would be grateful for your help!
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.
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
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.
Here is what I am trying to do. I am trying to create a workbook based on a template named by the title and to create a workbook for each row. And for the macro to loop until all rows have been depleted.
The deliverables that I want at the end are 3 excel documents named (Alpha.xlsx, Beta.xlsx, Gamma.xlsx) with the corresponding values from access plugged into their corresponding cells in their corresponding workbook. The subsequent math is there because I need to be able to manipulate the values once they are in excel.
Here is some of the research that I've found that I haven't quite been able to make much sense of due to my lack of experience coding in vba.
Links
(I can't post more than 2 so I'll keep the number of articles terse):
Research: databasejournal.com/features/msaccess/article.php/3563671/Export-Data-To-Excel.htm
Example Database/Spreadsheet:
http://www.sendspace.com/file/iy62c0
Image Album (has a picture of the database and the template in case you don't want to download):
http://imgur.com/pytPK,PY8FP#0
Any help will be much appreciated! I've been reading up and trying to figure out how to get this to work #.#
This isn't complete, but should help you get started...
Option Compare Database
Option Explicit
'Enter Location of your Template Here
Const ExcelTemplate = "C:\MyTemplate.xltx"
'Enter the Folder Directory to save results to
Const SaveResutsFldr = "C:\Results\"
Sub CreateWorkbook()
Dim SaveAsStr As String
Dim ExcelApp, WB As Object
'Create Reference to Run Excel
Set ExcelApp = CreateObject("Excel.Application")
'Create Reference to your Table
Dim T As Recordset
Set T = CurrentDb.OpenRecordset("tblData")
'Loop through all Record on Table
While Not T.BOF And T.EOF
'Open Your Excel Template
Set WB = ExcelApp.Workbooks.Open(ExcelTemplate)
'Enter your data from your table here to the required cells
WB.Worksheets("NameOfYourWorkSheet").Range("A1") = T("numValue1")
'Repeat this line for each piece of data you need entered
'Changing the Sheet name, cell range, a field name as per your requirements
'WB.Wor...
'WB.Wor...
'Save and Close the Workbook
SaveAsStr = SaveResutsFldr & T("Title") & ".xlsx"
WB.SaveAs SaveAsStr
WB.Close
Set WB = Nothing
'Move to the Next Record
T.MoveNext
Wend
'Close down the Excel Application
ExcelApp.Quit
Set ExcelApp = Nothing
End Sub