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!
Related
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!
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"
I have a situation where I need to pass a variable from Word to Excel via a macro. The reasoning is a little complicated, but basically I want to use this as a way to create a customized index. I point that out in case there might be an easier way to create the index. This particular index is created based on the paragraph a word or phrase is found in and not the page number. There doesn't appear to be a way to do this in Word.
So, I want to highlight the word or phrase, press my macro combo (or add a right click menu item to do it) which assigns the highlighted text to a variable and then sends it to one of three open worksheets (there will be 3 sections to the index) and adds it to the bottom row of the spreadsheet. From there I need to alphabetize the list and combine all the same phrases into one cell.
I know how to send items from Excel to Word, but when I try to research the idea of sending from Word to Excel all I can find are examples of Excel retrieving something from Word and returning it back to Excel. This is not applicable to what I'm doing. There will be thousands of index entries (before I combine them).
If it isn't possible to go directly from Word to Excel then I suppose I could send the selected text to another another document, but again I don't know how to do this.
Please help!
I did not understand why you need to work in Excel from Word, but I prepared a piece of code to show you how you can (easily) acces Excel object and work using its objects:
Sub testWorkingInExcel()
Dim objEx As Excel.Application, wb As Excel.Workbook, W As Excel.Workbook, sh As Excel.Worksheet
Dim lastEmptyRow As Long, boolFound As Boolean
On Error Resume Next
Set objEx = GetObject(, "Excel.Application") 'find Excel open session if any
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
Set objEx = CreateObject("Excel.Application")
Set wb = objEx.Workbooks.Open("Workbook full name")
Else
For Each W In objEx.Workbooks
If W.Name = "Needed workbook" Then
Set wb = W: boolFound = True: Exit Sub
End If
Next
If Not boolFound Then Set wb = objEx.Workbooks.Open("Workbook full name")
End If
On Error GoTo 0
Set sh = wb.Worksheets("NeededSheet")
lastEmptyRow = sh.Range("A" & sh.Rows.Count).End(xlUp).Row
'Now, you have the last empty row of the needed page and you can do
'whatever you could do directly in Excel...
End Sub
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 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.