Results 1 to 25 of 25

Thread: Creating and formatting word tables in VB

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Question Creating and formatting word tables in VB

    Hi guys

    I've tried to search this but I found nada.

    How do I create a tables in Word from my VB app and after that insert some text/pictures into the table?

    Regards,
    Sveegaard

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    If you record a macro doing what you need and then stop recording, look at the module code it generates and you will see how word does it.
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Doh, didn't think about that!

    Well, I don't think about very often

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    Not thinking is the #1 cause of headaches say the 4 out of 5 doctors that were surveyed.
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Hehe..

    Hmm, but VBA and VB 6 'talk' too different - why can't I just copy&paste it or CreateObject? :S

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

    Re: Creating and formatting word tables in VB

    How do you mean? Are you needing this to execute from a VB 6 program or ???
    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
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Yearh, I need to exe it in VB6

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    Ok, here are two examples for you. The first will paste in to the document whatever is on the clipboard. The second will create a new table.
    VB Code:
    1. Option Explicit
    2. 'Add a areference to MS Word xx.0 Object Library
    3. Private Sub Command1_Click()
    4.     Dim oApp As Word.Application
    5.     Dim oDoc As Word.Document
    6.     Set oApp = New Word.Application
    7.     oApp.Visible = True
    8.     Set oDoc = oApp.Documents.Add
    9.     oDoc.Activate
    10.     oDoc.Select
    11.     oApp.Selection.Paste 'Pastes whatever is on the clipboard.
    12.     'Do whatever.
    13.     'Clean up objects
    14.     oDoc.SaveAs FileName:="C:\Command1.doc"
    15.     oDoc.Saved = True
    16.     oDoc.Close SaveChanges:=False
    17.     Set oDoc = Nothing
    18.     oApp.Quit False
    19.     Set oApp = Nothing
    20. End Sub
    21.  
    22. Private Sub Command2_Click()
    23.     Dim oApp As Word.Application
    24.     Dim oDoc As Word.Document
    25.     Set oApp = New Word.Application
    26.     oApp.Visible = True
    27.     Set oDoc = oApp.Documents.Add
    28.     oDoc.Activate
    29.     oDoc.Select
    30.     oDoc.Tables.Add Range:=Selection.Range, NumRows:=10, NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, _
    31.     AutoFitBehavior:=wdAutoFitWindow
    32.     With Selection.Tables(1)
    33.         If .Style <> "Table Grid" Then
    34.             .Style = "Table Grid"
    35.         End If
    36.         .ApplyStyleHeadingRows = True
    37.         .ApplyStyleLastRow = True
    38.         .ApplyStyleFirstColumn = True
    39.         .ApplyStyleLastColumn = True
    40.     End With
    41.     'Do whatever.
    42.     'Clean up objects
    43.     oDoc.SaveAs FileName:="C:\Command2.doc"
    44.     oDoc.Saved = True
    45.     oDoc.Close SaveChanges:=False
    46.     Set oDoc = Nothing
    47.     oApp.Quit False
    48.     Set oApp = Nothing
    49. End Sub
    Last edited by RobDog888; Oct 30th, 2005 at 02:33 PM.
    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Ok Thx

    Now VB says 'Method 'Add' of object 'Tables' failed'

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    What version of Word are you running?

    I added oDoc.Select to the example right after oDoc.Activate to help.
    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

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    I'm running Word 2002

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    Did you try the updated code? I edited my last post in case you didnt see the change.
    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

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Didn't notice that

    But now it's complaining about 'with' :S

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    What is the complaint?
    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

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Something about variable object at the tables.add

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    You did see that the oDoc.Tables.Add runs across two lines of code with the line continuation character? So it hasnt yet created a table in a word doc yet?
    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

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Yup, noticed that.

    No, it doesn't make a table and Word is trying to save a .dot :S

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    Your saying that with this code your app is trying to save a dot (Template)?
    VB Code:
    1. Option Explicit
    2. 'Add a areference to MS Word xx.0 Object Library
    3. Private Sub Command2_Click()
    4.     Dim oApp As Word.Application
    5.     Dim oDoc As Word.Document
    6.     Set oApp = New Word.Application
    7.     oApp.Visible = True
    8.     Set oDoc = oApp.Documents.Add
    9.     oDoc.Activate
    10.     oDoc.Select
    11.     oDoc.Tables.Add Range:=Selection.Range, NumRows:=10, NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow
    12.     With Selection.Tables(1)
    13.         If .Style <> "Table Grid" Then
    14.             .Style = "Table Grid"
    15.         End If
    16.         .ApplyStyleHeadingRows = True
    17.         .ApplyStyleLastRow = True
    18.         .ApplyStyleFirstColumn = True
    19.         .ApplyStyleLastColumn = True
    20.     End With
    21.     'Do whatever.
    22.     'Clean up objects
    23.     oDoc.SaveAs FileName:="C:\Command2.doc"
    24.     oDoc.Saved = True
    25.     oDoc.Close SaveChanges:=False
    26.     Set oDoc = Nothing
    27.     oApp.Quit False
    28.     Set oApp = Nothing
    29. End Sub
    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

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Now it works

    But when I open the document, it says that it has overwritten normal.dot - however, it hasn't :S

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Hmm, got no clue how to put in text in a table.

    Tried:
    VB Code:
    1. oDoc.Tables(1).Cells(1,1)="Test"
    But no result

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    You must have something else going on since this does not reference normal.dot at all. Did you place this code in the normal.dot template or in your vb 6 program?

    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

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    The code is placed in my VB6 program.

    But more important is how to insert a text into a certain cell

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    VB Code:
    1. oDoc.Tables(1).Cell(1, 1).Range.Text = "This is row 1 colum 1 in my first table."
    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

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Creating and formatting word tables in VB

    Now it - once again - says that 'Add' is not optional, just as #9

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating and formatting word tables in VB

    This is really weird. The code is very basic and should not be giving us all these problems. Perhaps your Word or word reference is messed up. Could you try doing a Detect & Repair from the Help menu and then an Office Update?
    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

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