export DGV to word ~ excel - Page 2-VBForums
Page 2 of 2 FirstFirst 12
Results 41 to 71 of 71

Thread: export DGV to word ~ excel

  1. #41
    New Member
    Join Date
    May 2011
    Posts
    6

    Re: export DGV to word ~ excel

    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.

  2. #42

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    try this:
    Attached Files Attached Files

  3. #43
    New Member
    Join Date
    May 2011
    Posts
    6

    Re: export DGV to word ~ excel

    That works! Thank-you very much

  4. #44

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    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

  5. #45
    New Member
    Join Date
    May 2011
    Posts
    6

    Re: export DGV to word ~ excel

    Hi Paul,

    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.

    Thankyou,

    Verra
    Last edited by Verra; May 25th, 2011 at 08:07 PM.

  6. #46
    New Member
    Join Date
    Oct 2011
    Posts
    3

    Re: export DGV to word ~ excel

    I'm thinking in adapt this example to export one list to excel but i get the following error:

    haven't changed a thing. only saw the dependencies if the excel dependence was at fault but it was there. the 'export to word' button works perfectly

  7. #47
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: export DGV to word ~ excel

    Hi .Paul

    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

  8. #48

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    the problem with a document that IS open in Word is finding a way to refer to it.

    2/ 'align right
    oTable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight

  9. #49
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: export DGV to word ~ excel

    Thank you paul for your help.

    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?

  10. #50

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    ok. using my code, change:

    vb.net Code:
    1. ' Create Word Application
    2. Dim oWord As Word.Application = DirectCast(CreateObject("Word.Application"), Word.Application)

    to:

    vb.net Code:
    1. Dim oWord As Word.Application
    2. Try
    3.     ' Get Word Application
    4.     oWord = DirectCast(GetObject(, "Word.Application"), Word.Application)
    5. Catch ex As Exception
    6.     MsgBox("Can't find Word Document", MsgBoxStyle.Information)
    7.     Return
    8. End Try
    Last edited by .paul.; Oct 12th, 2012 at 12:46 PM.

  11. #51
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: export DGV to word ~ excel

    Thank you paul.

    Unfortunately the DGV is still added to a NEW opened Word document. The DGV is NOT added to the Word document that is already open.

    Maybe the problem is due to this code in your code:

    Code:
    Dim oDoc As Word.Document = oWord.Documents.Add

  12. #52

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    ok. post the code you tried. i'll see if i can find the error.

  13. #53

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    Quote Originally Posted by redseujac View Post
    Maybe the problem is due to this code in your code:

    Code:
    Dim oDoc As Word.Document = oWord.Documents.Add
    yes, almost definitely. i don't know the exact code you need to use until i've tried the code you're using in vb

  14. #54

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    Dim oDoc As Word.Document = oWord.ActiveDocument

  15. #55
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: export DGV to word ~ excel

    referring to the ActiveDocument did the trick finally

    Thank you veru much, paul.

    Now we have 2 sample codes: 1 to copy a DGV to a new Word document and 1 to copy it (append it) to an open Word document.

    Nice

  16. #56

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    Quote Originally Posted by redseujac View Post
    referring to the ActiveDocument did the trick finally

    Thank you veru much, paul.

    Now we have 2 sample codes: 1 to copy a DGV to a new Word document and 1 to copy it (append it) to an open Word document.

    Nice
    did you see the insert into bookmarks example in post #42?

  17. #57
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: export DGV to word ~ excel

    Yes, I did paul, but I couldn't use the code in that example.

    But you are right indeed: there are not 2 but 3 examples. My fault

  18. #58

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    Quote Originally Posted by redseujac View Post
    Yes, I did paul, but I couldn't use the code in that example.

    But you are right indeed: there are not 2 but 3 examples. My fault
    no problem... that's the whole point of this thread, to answer questions about exporting dgvs to Word + Excel

  19. #59
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: export DGV to word ~ excel

    paul

    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.

    Thanks.

  20. #60
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: export DGV to word ~ excel

    Hello paul

    1) Still no answer possible to my post #59?

    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?

    Thanks & best regards

    Jacques

  21. #61

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    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.

  22. #62
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: export DGV to word ~ excel

    Here are the errors I get from this code
    Name:  DGV_Errors.jpg
Views: 99
Size:  146.3 KB

  23. #63
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: export DGV to word ~ excel

    ok I got it

    change
    Code:
    Excel.ApplicationClass xlapp = new Excel.ApplicationClass();
    to this
    Code:
    Excel.Application xlapp = new Excel.Application();

  24. #64

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    you're not using excel 2007 then... sorry i didn't get back to you earlier.

  25. #65
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: export DGV to word ~ excel

    Not a problem..yeah I'm using 2010

  26. #66
    Lively Member
    Join Date
    Sep 2013
    Posts
    75

    Re: export DGV to word ~ excel

    HI,

    I'm posting the questions I have in this topic as I'm trying to adapt the code paul has provided in the zip file.

    I've got VS2010 and MS Office 2010 as environment. I'm only using the Export to EXCEL part.

    My question: I'd like to be able to chose the worksheet I'm pasting the selection on.

    I've already done this adjustment from your code to be able to chose the starting cell:

    Code:
    Dim range As Excel.Range= xlWorkSheet.Range("I62").Resize(items.length,headerText.length)
    Which doesn't use the alphabet() array anymore.

    As to which sheet is concerned I tried this:

    Code:
    Dim xlWkSheet As Excel.Worksheet=Ctype (xlWorkBook.Worksheets(Index:=1), excel.Worksheet)
    It has worked, but when I tried to change the "Index" it caused an error and range.select() was highlighted.

    How would one chose what worksheet is the target ?

  27. #67
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,546

    Re: export DGV to word ~ excel

    Hey .paul.,

    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:

    range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone

  28. #68

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    try this:

    Code:
    xlApp = CreateObject("Excel.Application")
    ...
    range.Borders(xlApp.XlBordersIndex.xlDiagonalUp).LineStyle = xlApp.XlLineStyle.xlLineStyleNone

  29. #69
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,546

    Re: export DGV to word ~ excel

    Thanks. I get this error:

    Public member 'XlBordersIndex' on type 'ApplicationClass' not found.
    on this line of code:

    objRange.Borders(xlApp.XlBordersIndex.xlDiagonalUp).LineStyle = xlApp.XlLineStyle.xlLineStyleNone

  30. #70

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,339

    Re: export DGV to word ~ excel

    all of the members of the XlBordersIndex enum have an integer value, as do the members of the XlLineStyle enum, + the XlBorderWeight enum.

  31. #71
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,546

    Re: export DGV to word ~ excel

    hey .paul.

    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?

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.