http://social.msdn.microsoft.com/For...d-9bccf025f007 - I found this but when I try and adapt it to my code the blue underline appears, I'm thinking I don't have the correct references added, but I wouldn't have a clue which ones I need.
if you're not sure about the references, you can check them in the example. it uses Microsoft Word 12.0 Object Library which you'll find on the references COM tab
Nevermind, I just figured out why it wasn't working. It doesn't like being split up onto a couple of lines.
I've gone through and added more grid views to the code you've given me, and I'm coming up with an error which I'm sure the answer to is simple; but my thinking currently isn't at its best haha.
It's saying that 'dgvs' isn't declared, when it definitely is. Here's the part where the object is declared:
Code:
'Bookmarks are "grid1" (relates to DataGridView1), + "grid2" (relates to DataGridView2)
Dim dgvs(,) As Object = {{dgAssetsSummary, "AssetsSummary"}, {dgLiabilitiesSummary, "LiabilitiesSummary"}, {dgAssetsFull, "AssetsFull"}, & _
{dgLiabilitiesFull, "LiabilitiesFull"}, {dgPAEFull, "PAEFull"}, {dgGoalsMediumTerm, "GoalsMediumTerm"}, & _
{dgGWStopped, "GWStopped"}, {dgGWAdded, "GWAdded"}, {dgFLSum, "FLSums"}}
Sorry to trouble you about this again, I just can't see why it isn't working.
Two more questions about how to export DGV to Word.
1) Your sample code exports the DGV to a new Word document. What's the code for exporting the DGV to a document that's opened in Word, I mean append the DGV to the end of that open Word document ? Attention: the document must not be opened, it IS open in Word.
2) What is the code to align the text in the Word table cells to the right instead of left ?
I applied successfully your sample code, but I couldn't figure out how to do what I asked in the previous questions.
Thanks
Jacques
I already applied sucessfully your answer to my second question.
As to the first problem - copying the DGV to an opened Word document - I have the following code in VBA to copy the contents of a worksheet to an open Word document and it's working like a charm. Unfortunately I'm not able to write similar code in Visual Basic.
Code:
Sub CopyFromExcel2VisibleOpenWordDoc()
Dim WrdApp As Object
Dim WrdDoc As Object
Dim LastRow As Long
Dim lRow As Long
Dim iCol As Integer
LastRow = 0
On Error Resume Next
Set WrdApp = GetObject(, "Word.Application")
If WrdApp Is Nothing Then
MsgBox "MS Word is not available!" _
& vbCrLf & "Start MS Word" _
& vbCrLf & "and put the cursor at the right place" _
& vbCrLf & "in the right document!", vbExclamation
End If
For iCol = 1 To 6
lRow = Cells(65536, iCol).End(xlUp).Row
If lRow > LastRow Then LastRow = lRow
Next iCol
'Copy from last row to begin table
ActiveSheet.Range(Cells(1, "A"), Cells(LastRow, "F")).Copy
With WrdApp
.Visible = True
.Activate
Set WrdDoc = WrdApp.ActiveDocument
'WrdDoc.Show
With WrdDoc
.Activate
.Application.Selection.TypeParagraph
.Application.Selection.Paste
End With
End With
Application.CutCopyMode = False
End Sub
Do you see something in that code that gives you an idea?
After the DGV has been exported to MS Word table, I experience an annoying problem: the content in the Word table cells is not formatted as I want it to be.
After the date, the time is added ("00:00:00") and the numbers have too many decimal places (instead of 2).
I have tried to resolve this problem by first converting the DGV cells values using standard numeric format strings (ToString method) before pasting them to the Word table, but I am not very happy with this method, because then my DGV values aren't usable anymore (converted to strings) and I am forced to remove them all from the DGV and to enter all the data again, if I want to calculate results once more.
I guess it would be better to format the Word table cells content directly after pasting the DGV to Word, but honestly I don't know how to manage that.
I would be grateful if you could give me some code to insert in your existing code to format the Word table cells content for dates and numbers the way the dates don't have the time added and the decimal places for the numbers are limited to 2.
2) How to adapt your code to copy NOT the ENTIRE DataGridView to Word, but only the SELECTED area (with mouse e.g.) of the DataGridView, thus the selected columns and rows?
1/ i'm not sure of how to tackle that. it'd probably be best to format the dateTime in the items array after reading the dgv
2/ that's a whole new program. there are plenty of examples either here on vbforums or you can find them through google. sorry i couldn't be more help.
I've just come across your code and it work's great. I am trying this using late binding. Please let me know how I would do the following using late binding:
another question. I have a datagridview combobox column. When I export to Excel the Integer value of the combo is printed. How do I go about getting the text value for combobox columns in my export code?