Need to select several rows in word table from excel and then format
Copied table from excel to word
Can select a single row
Need to format selected row by adding space before in each cell
Need to do for several rows prefer to select range of cells and format all the same
Code:
Set objword = createobject(“word.application”)
Set objdoc = objword.documents.add
With objword
‘Lines of code for pagesetup working ok
‘Then paste excel table to word working ok
.selection paste
.visible = true
‘Have code sorted to select single row working ok
.activedocument.table(1).row(3).select
End with
Need to have code to select several rows
Then format by adding space before in each cell or entire row
Everything tried gives me unsupported function
Notes:
Will copy the Selected table in Excel to Word Document
Loop through all it's cell
You can change their Text , add space in front.
Skip rows in the loop using If condition.
Code:
Sub tb_copy()
Selection.Copy
Set objWord = CreateObject("word.application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
With objDoc
.Paragraphs(1).Range.PasteExcelTable False, False, False
For i = 1 To .tables(1).Rows.Count
For j = 1 To .tables(1).Columns.Count
Debug.Print .tables(1).cell(i, j).Range.Text
Next
Next
End With
End Sub
Problem:
.Selection.paste does not work
Need with statement with ObjDoc
Related
I'm designing an excel worksheet, where I can add the text from certain cells to a new word document if a condition for the each cell is met.
My code pastes the text from the cell to the new word document. But it always replaces the text from the previous cell. So only the last cell is visible. How can I change that?
Private Sub CommandButton1_Click()
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = WrdApp.Documents.Add
a = Worksheets("Tabelle1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 6 To a
If Worksheets("Tabelle1").Cells(i, 5).Value = "Ja" Then
Worksheets("Tabelle1").Cells(i, 4).Copy
WrdDoc.Paragraphs(1).Range.PasteSpecial xlPasteValues
End If
Next
Application.CutCopyMode = False
End Sub
Your problem is that you are potentially pasting 6 times into the exact same location, leading to the text at that location being replaced each time. You need to think about what you would do if you were doing this task without code, and then write code that does the same.
You could start by changing:
WrdDoc.Paragraphs(1).Range.PasteSpecial xlPasteValues
to
WrdDoc.Characters.Last.PasteSpecial xlPasteValues
But you will still need to add something between each value you paste.
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 having some issues with some VBA code. What I am trying to do is export code from Excel cells and import them into a Word document in a text field.
Here is the code I have.
Sub test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\Acer Windows 7\Desktop\test.docx"
With objWord.ActiveDocument
.Text1.Value = ws.Range("A1").Value
.Text2.Value = ws.Range("B1").Value
.Text3.Value = ws.Range("C1").Value
End With
End Sub
This code takes static cells and exports them into a Word document. What I need is a link or button on each row that will export that code from said row and put them into the word document.
Example if I click the link/button on row 4 it takes the data from C4, E4, F4
Is this possible? I am not sure how to do so.
If there are many rows it can be a bit cumbersome to add a button for each row. I suggest to only use 1 button that exports the selected row. So you only have to maintain 1 button/sub instead of many.
Cells(Row, Column) can be used here.
Sub test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\Acer Windows 7\Desktop\test.docx"
With objWord.ActiveDocument
.Text1.Value = ws.Cells(Selection.Row, 1).Value
.Text2.Value = ws.Cells(Selection.Row, 2).Value
.Text3.Value = ws.Cells(Selection.Row, 3).Value
End With
End Sub
The code above would always use the row of cell which is selected. So first you select the row that you like to export and then you press the button that runs test().
I am trying to pull data from Excel and place it into a word Text Form Field. The code below works except that it will not pull data from the cell I specify -- it pulls the text from a different cell. I can see that the correct cell is being selected in Excel because it is outline, but it simply does not pull the data from it. I have messed around trying different cells and with some it pulls the data I want and with others it pulls data from a different cell.
Anyone know reaons why this might be happening and how I can resolve it? Thanks.
Private Sub CommandButton1_Click()
Dim wrdApp
Dim wrdDoc
Dim Data
ActiveSheet.Cells(8, 1).Activate
Data = Selection.Cells(8, 1).Value
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("H:\jpmDesk\Desktop\VBA tester.docx")
With wrdDoc
wrdApp.ActiveDocument.Bookmarks("Text1").Select
wrdApp.Selection.Range.Text = Data
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
Change
Data = Selection.Cells(8, 1).Value
to
Data = Selection.Value
Replace existing code:
ActiveSheet.Cells(8, 1).Activate
Data = Selection.Cells(8, 1).Value
With this:
Data = Range("A1").Offset(7, 0).Value
Cells(8,1).Value is not the same as Range("A1").Offset(7,0).Value
By using the offset method of the Range object, you can now retrieve values without actually selecting the cell.
Step through your code in VBE by using F8 to see how this works.
The following code copies a single value into a bookmark in Word. I need it to copy a range of values like "A6:G20".
Sub test()
Dim objWord As Object
Dim ws As Worksheet
Set ws = Workbooks("Portfolio1").Sheets("Print")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "D:Q.docx" ' change as required
With objWord.ActiveDocument
.Bookmarks("monthtable").Range.Text = ws.Range("C6").Value ' here I need range of values to be selected instead of a single cell
End With
Set objWord = Nothing
End Sub
If suitable, you could copy and paste the range:
Range("A6:G20").Copy
.Bookmarks("monthtable").Range.PasteExcelTable False, False, False
There are a number of other Paste methods if you don't wish to paste as an Excel table. Use Word's VB Editor to discover these, or the Word Macro Recorder.
This is of course the important part:
With objWord.ActiveDocument
.Bookmarks("monthtable").Range.Text = ws.Range("C6").Value ' here i need range of values to be selected instead of a single cell
End With
Here you need to cycle through the ws.Range("..."), and for each cell in that range, concatenate that value onto the .Bookmarks.Range.Text value (Rather than setting it equal, which would overwrite). It's probably a good idea to first concatenate this into a string variable and then set the bookmark to that string variable's value, since that would avoid some potential interop issues.