none
Create a Word Document with multiple Tables using Visual Studio 2010 RRS feed

  • Question

  • I would like to create a Word document with 5 separate Tables (4 columns, 20 rows) using Visual Studio 2010.  I've looked at some of the other code samples in the forums and none of them are working for me, I get a bunch of syntax errors.

    Could some please provide me an example of how I can do this?

    Thanks in advance,

    Jim


    James Hutchinson

    Sunday, November 30, 2014 2:12 AM

All replies

  • Some VBA code to get you started:

    Sub Demo()
    Dim Rng As Range, i As Long
    With ActiveDocument.Range
      For i = 1 To 4
        If i > 1 Then .InsertAfter vbCr
        Set Rng = .Paragraphs.Last.Range
        Rng.Collapse wdCollapseEnd
        .Tables.Add Range:=Rng, Numrows:=20, NumColumns:=4
      Next
    End With
    End Sub

    The code inserts the four tables at the end of the active document. If working with a new document, you might want to delete 'If i > 1 Then .InsertAfter vbCr', so the first table starts right at the top of the document.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, November 30, 2014 6:10 AM
  • Hello Jim,

    You may find the How to automate Microsoft Word to create a new document by using Visual C# article helpful. It provides a sample code in C#.

    Sunday, November 30, 2014 11:37 AM
  • Eugene,

    I did look at those instructions previously.  However, those instructions are not for Visual Studio 2010, they are for Visual Studio .NET or 2005.  I do not see an option to create a new C# project in VS 2010.

    I did try to use that code and I got a bunch of syntax errors when trying to using it in a Visual Basic project in VS 2010.  If you know how to modify that code for Visual Basic and VS 2010, then I'm willing to try it again.


    James Hutchinson

    Monday, December 1, 2014 12:47 AM
  • Paul,

    I get three errors when I try to compile your code:

    1)  "System.Data.Range is not accessible in this context because it is 'Friend'."

    2) "ActiveDocument is not declared.  It may be inaccessible due to its protection level."

    3) wdCollapseEnd is not declared.  It may be inaccessible due to its protection level."

    Thanks,

    Jim


    James Hutchinson


    • Edited by jhutch03 Monday, December 1, 2014 12:58 AM changed a word
    Monday, December 1, 2014 12:58 AM
  • As I said in my previous post, it's VBA code. You'd need to adapt it for Visual Studio. Since your first post said nothing of the language you're issuing, I assumed it might have been Visual Basic, for which migration is fairly straightforward. Obviously, you'd also need to set references to the Word Object model and reference its ActiveDocument property accordingly. The fact you're getting errors like 'ActiveDocument is not declared' suggests you haven't done that. In any event, you need to use code like wdApp.ActiveDocument, where wdApp refers to your Word session. For example:

    Imports Word
    Friend WithEvents wdApp As Word.Application

    You can then reference wdApp.ActiveDocument. And, if you need to create the document as well, you'll need to use Word's Documents.Add method. If you're doing that, though, you might do better to start with a template already containing your four tables and whatever other default content you want the document to have. Then all you need do is reference the template as a parameter of the Documents.Add method.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, December 1, 2014 2:01 AM
  • Paul,

    I'm using Visual Basic.  I tried your Imports Word statement above, but I get an error -- "Imports 'Word' doesn't contain any public member or cannot be found".   I tried it inside the Form1 Class and above the Form1 Class and I got the same error.

    Also, when I try to type "Word.Application" in your statement above, Visual Studio automatically changes it to something else.  If there is something else I need to do before I can use your 2 statements above, please let me know what it is. 


    James Hutchinson

    Friday, December 5, 2014 6:15 AM
  • I don't use Visual Studio, so I can't really give detailed advice - the suggestions in my previous post were based on what I'd gleaned from web searches. As I said, though, the code I posted is Word's own VBA, which has a lot in common with Visual Studio's Visual Basic and has all the methods & procedures you need to use. Your problems seem to relate to basic automation issues, for which see:
    https://support.microsoft.com/kb/316383

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, December 5, 2014 8:08 AM