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.
Related
I am trying to create a VBA script that copies a cell value when a form control checkbox in the corresponding row is "checked". There are around 116 rows, and I only want to copy the cell value for the checked rows.
For example, my checkboxes are in cells D6:D122. If rows D6, D8, and D10 are checked, I want to copy the values within cells C6, C8, and C10, alphabetize the results and paste them into a newly generated Word document when a command button is clicked. I have figured out how to generate a new word document, but I have trouble copying over the cell values and alphabetizing them.
This is my code as of now:
Sub CommandButton1_click()
Dim wdApp As Word.Application
Dim var as Variant
Set wdApp = New Word.Application
With wdApp
.Visible = True
.Activate
.Documents.Add
End With
End Sub
One of the simpler solutions:
Sub CommandButton1_click()
Dim cl As Range, txt As String
For Each cl In ThisWorkbook.Worksheets(1).Range("D6:D122")
If cl Then txt = txt & vbLf & cl.Offset(0, -1)
Next cl
If Len(txt) > 0 Then ' show Word if you have something to output
Dim wdApp As New Word.Application ' declare and create object at once
With wdApp
.Documents.Add ' the added document automatically becomes active
.Selection.TypeText Mid(txt, 2) 'remove extra (lead) vbLf and output text to Word
.ActiveDocument.Range.Sort
.Visible = True 'show Word after processing to improve performance
End With
Else
MsgBox "There is nothing to output", vbInformation + vbOKOnly
End If
End Sub
So we have this table we are using at the office. I changed the Column Name and Content for confidentiality purposes.
We're trying create a Word Document for each ID consisting of all the Name's and Surnames for that ID only from our Excel file.
i.e. A new Word Document is created for ID1. The contents are all the Names and Surnames only for that ID1 excluding the Column Name. Another Word Document will be created for the next ID available until all IDs have their own document,
So far this is what I got:
Sub test()
Dim copyRng As Range
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set copyRng = Range("B2:C" & lastrow)
Range("B2:C" & copyRng.Rows.Count).Select
Selection.Copy
'Declare Word Variables
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
'Create a new instance of Word
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
'Create a new Document in the Word Application
Set WrdDoc = WrdApp.Documents.Add
WrdDoc.Activate
WrdDoc.Range(WrdDoc.Characters.Count - 1).Paste
End Sub
I can't seem to copy only the rows for a specific ID.
Can anyone suggest a better solution copy only the cells based on the IDs?
I simplified my problem in the meantime.
First I select the cells I want to be copied over to a Word Document.
Then I run this code:
Selection.Copy
'Declare Word Variables
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
'Create a new instance of Word
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
'Create a new Document in the Word Application
Set WrdDoc = WrdApp.Documents.Add
WrdDoc.Activate
With WrdDoc.Range(WrdDoc.Characters.Count - 1).Characters.Last
.PasteExcelTable False, True False
With .Tables(1)
.AutoFitBehavior wdAutoFitWindow
End With
.InsertAfter Chr(1)
End With
This way, I just have to highlight the cells I want to be copied over to Word and Run the Macro. The Macro will create a new Word Document for me.
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.