Page 2 of 2 FirstFirst 12
Results 41 to 65 of 65

Thread: export DGV to word ~ excel

  1. #41
    New Member
    Join Date
    May 11
    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

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

    Re: export DGV to word ~ excel

    That works! Thank-you very much

  4. #44

  5. #45
    New Member
    Join Date
    May 11
    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 11
    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 12
    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

  9. #49
    New Member
    Join Date
    Oct 12
    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
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,519

    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

  11. #51
    New Member
    Join Date
    Oct 12
    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

  13. #53

  14. #54

  15. #55
    New Member
    Join Date
    Oct 12
    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
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,519

    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 12
    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
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,519

    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 12
    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 12
    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
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,519

    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 12
    Posts
    99

    Re: export DGV to word ~ excel

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

  23. #63
    Lively Member
    Join Date
    Sep 12
    Posts
    99

    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

  25. #65
    Lively Member
    Join Date
    Sep 12
    Posts
    99

    Re: export DGV to word ~ excel

    Not a problem..yeah I'm using 2010

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
  •