Results 1 to 10 of 10

Thread: Copy MSFlexGrid Value to Word

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Location
    The Nederlands
    Posts
    33

    Copy MSFlexGrid Value to Word

    A few weeks ago I put on a thread and I tought my problem was solved. Unfortunaly it isn't. I use the following code to get the MSFlexGrid value into a MSWord-document. The code does the job and there is an nice table in my MSWord-document. But I would like to be able to work with Bookmark because I'll be able to put the table where I want on the MSWord-document.
    If I use a Bookmark now, I only get the value from row.1 and Col.0.
    But I really don't know how. Does anybody has a suggestion?

    VB Code:
    1. Private Sub CmdCopyToWord_Click()
    2.  
    3. Dim objWord As Word.Application
    4. Dim objDoc As Word.Document
    5. Dim strData As String
    6.    
    7. With GrdInfo
    8.      .Row = 1
    9.      .Col = 0
    10.      .RowSel = .Rows - 1
    11.      .ColSel = .Cols - 1
    12.      strData = .Clip
    13. End With
    14.  
    15. 'The Clip property separates Rows with a carriage return.  These
    16. 'need to be converted to Tabs as well in order to create the Word Table properly.
    17. strData = Replace(strData, vbCr, vbTab)
    18.    
    19. Clipboard.Clear
    20. Clipboard.SetText strData
    21.    
    22. 'Create the Word Table by pasting the Grid Text into a document
    23. 'and then use the ConvertToTable method
    24. Set objWord = New Word.Application
    25. Set objDoc = objWord.Documents.Add
    26. objWord.Visible = True 'hiermee wordt Word zichtbaar
    27. objDoc.Range.Paste
    28. objDoc.Range.ConvertToTable wdSeparateByTabs, 5, 3
    29.  
    30. 'Copy the Word Table to the Clipboard.  The user can then Paste
    31. 'the table into their word documents.
    32. objDoc.Range.Copy
    33.    
    34. objDoc.Close False
    35. 'Set objDoc = Nothing
    36.    
    37. objWord.Quit False
    38. 'Set objWord = Nothing
    39.  
    40. End Sub

  2. #2
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Copy MSFlexGrid Value to Word

    I have used bookmarks before to jump to certain areas within a document.
    Never had a problem with it.

    VB Code:
    1. With objWord
    2.         .DisplayAlerts = wdAlertsNone
    3.  
    4.         ' Jump to and populate the relevant Bookmarks in the document.
    5.         .Selection.Goto What:=wdGoToBookmark, Name:="Date"
    6.         .Selection.TypeText Text:=Format$(Date, "DD/MM/YYYY")
    7.  
    8.         .Selection.Goto What:=wdGoToBookmark, Name:="StorageNo"
    9.         .Selection.TypeText Text:=Format$(frmAmend.lblStorageNo, "00000")
    10.  
    11.         .Selection.Goto What:=wdGoToBookmark, Name:="OrderNo"
    12.         .Selection.TypeText Text:=lblPO
    13.  
    14.         .Selection.Goto What:=wdGoToBookmark, Name:="SupplierNo"
    15.         .Selection.TypeText Text:=frmAmend.lblSupplier
    16.  
    17.         .Selection.Goto What:=wdGoToBookmark, Name:="WH"
    18.         .Selection.TypeText Text:=lblWH
    19.  
    20.         .Selection.Goto What:=wdGoToBookmark, Name:="Location"
    21.         .Selection.TypeText Text:=lblLoc
    22.  
    23.         .Selection.Goto What:=wdGoToBookmark, Name:="CartonsInStorage"
    24.         .Selection.TypeText Text:=lblCartonsInStorage
    25.  
    26.         .Selection.Goto What:=wdGoToBookmark, Name:="CartonCallOffText"
    27.         If Not Hanging Then
    28.             .Selection.TypeText Text:="No. of Cartons to Call-Off"
    29.         Else
    30.             .Selection.TypeText Text:="No. of Items to Call-Off"
    31.         End If
    32.         .Selection.Goto What:=wdGoToBookmark, Name:="CartonsCalledOff"
    33.         .Selection.TypeText Text:=txtCalloff(0)
    34.  
    35.         .Selection.Goto What:=wdGoToBookmark, Name:="NoTrips"
    36.         .Selection.TypeText Text:=txtCalloff(1)
    37.  
    38.         .Selection.Goto What:=wdGoToBookmark, Name:="CostPerTrip"
    39.         .Selection.TypeText Text:=txtCalloff(2)
    40.  
    41.         .Selection.Goto What:=wdGoToBookmark, Name:="TransportCost"
    42.         .Selection.TypeText Text:=lblTransCost
    43.  
    44.     End With

    That any help to you ?

  3. #3
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: Copy MSFlexGrid Value to Word

    Instead of using the selection object you can also do it this way.
    VB Code:
    1. Bookmarks.Item("Test").Range.Text = "Test"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Location
    The Nederlands
    Posts
    33

    Re: Copy MSFlexGrid Value to Word

    Hello TheBionicOrange,

    I do use Bookmarks for value from Textboxes without any problem. The problem lies in the value of a MSFlexGrid.

    I'm not (jet) familiar with the code you send me.
    Does the piece of code:
    VB Code:
    1. .Selection.Goto What:=wdGoToBookmark, Name:="Date"
    select a colum or somethning from my MSFlexGrid? Because in that case i'm saved!

  5. #5
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Copy MSFlexGrid Value to Word

    No it doesn't. It jumps the cursor to the location (bookmark) called "Date".
    Once I know the cursor is there I can populate it with whatever I want.
    In your case it would be he cell value from your flexgrid.

    I didn't know that Rob .... I'll make a note of that

  6. #6
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: Copy MSFlexGrid Value to Word

    Shortcuts are nice and a big performance gain from not having to use the selection object and move the caret around.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2006
    Location
    The Nederlands
    Posts
    33

    Re: Copy MSFlexGrid Value to Word

    Hello RobDog888,

    I'm to much of a VB6 beginner to understand what to do with the code gave me. Can you give me some explanation or perhaps a sample code (sorry, more sample code)?

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2006
    Location
    The Nederlands
    Posts
    33

    Re: Copy MSFlexGrid Value to Word

    Quote Originally Posted by RobDog888
    Shortcuts are nice and a big performance gain from not having to use the selection object and move the caret around.
    You lost me completely here! Sorry.

  9. #9
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: Copy MSFlexGrid Value to Word

    I was responding to thebonicorange's post/code.

    Can you be a bit more clear on what you need?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2006
    Location
    The Nederlands
    Posts
    33

    Re: Copy MSFlexGrid Value to Word

    Quote Originally Posted by RobDog888
    I was responding to thebonicorange's post/code.

    Can you be a bit more clear on what you need?
    Iwill try to. I use the code:

    VB Code:
    1. Private Sub CmdPrintBewaar_Click()
    2.  
    3. Dim msWord As Object
    4. Dim msWordDoc As Object
    5. Dim i As Integer
    6. Dim bkRange As Object
    7. Dim bkNamen As Variant
    8. Dim Tekst As Variant
    9. Dim WordWasOpen As Boolean
    10.  
    11. bkNamen = Array("qDatum", "qBedrijfsnaam", "gtav", "qAdres", "qnummer", "qtoevoeging", "qpostcode", "qplaats", "qland", "qbtwnummer", "qGrdInfo", "qbtwbedrag", "qSubTotaalN", "qSubTotaalJ", "qtotaalbedrag") 'Dit zijn de bladwijzers die je gebruik
    12. Tekst = Array(TxtDatum.Text, TxtBedrijfsnaam.Text, TxtTav.Text, TxtAdres.Text, TxtNummer.Text, TxtToevoeging.Text, TxtPostcode.Text, TxtPlaats.Text, TxtLand.Text, TxtBtwNummer.Text, GrdInfo, TxtBtwBedrag.Text, TxtSubTotaalN.Text, TxtSubTotaalJ.Text, TxtTotaalBedrag.Text) 'Dit zijn de variabelen die je gebruikt
    13.  
    14. On Error GoTo NoWordOpen
    15. WordWasOpen = False
    16. Set msWord = GetObject(, "Word.Application")
    17. On Error GoTo 0
    18. msWord.Visible = True
    19. msWord.WindowState = 1 'Const wdWindowStateMaximize = 1
    20. Set msWordDoc = msWord.Documents.Open("E:\DocumentenGenerator\RechnungSchilderStelle.dot") 'pad en naam van uw doc-bestand)
    21.  
    22. For i = LBound(bkNamen) To UBound(bkNamen)
    23.    
    24.   If msWord.ActiveDocument.Bookmarks.Exists(bkNamen(i)) Then
    25.      
    26.     Set bkRange = msWord.ActiveDocument.Bookmarks(bkNamen(i)).Range
    27.     bkRange.Text = Tekst(i)
    28.     msWord.ActiveDocument.Bookmarks.Add Name:=bkNamen(i), Range:=bkRange
    29.      
    30.   End If
    31.    
    32. Next
    33. msWord.ActiveDocument.SaveAs ("E:\DocumentenGenerator\" & "2006004.doc")
    34.  
    35. msWord.ActiveDocument.PrintOut False 'document wordt afgeprint
    36.  
    37. msWordDoc.Close
    38.  
    39. If Not WordWasOpen Then
    40.   msWord.Quit
    41. End If
    42.  
    43. Set msWord = Nothing
    44.  
    45. Exit Sub
    46.  
    47. NoWordOpen:
    48. WordWasOpen = False
    49. Set msWord = CreateObject("Word.Application")
    50. Resume Next
    51.  
    52.  
    53. End Sub


    And the result is only the value of Colum0 and Row1 (the first row = header).
    What I need is the value of the complete MSFlexGrid into my MSWord-document. And I need to be able to use Bookmarks to get the result into the MSWord-document on a specific location.

    Thanks for your help.

Posting Permissions

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



Click Here to Expand Forum to Full Width