+ Reply to Thread
Results 1 to 10 of 10

Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

  1. #1
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    Hey there, don't know what I'm meant to do here
    My aim here is to create a table in a microsoft word document, using one of my microsoft excel macros
    My code so far creates a word document

    Dim WordApp As Word.Application
    Set WordApp = New Word.Application
    WordApp.Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
    WordApp.Visible = True

    Then writes a few things, adds a paragraph

    WordApp.Selection.TypeText Text:="abcdefg"
    WordApp.Selection.TypeParagraph

    Now I want to make it create a table, with 2 columns and 9 rows, with normal black cell borders, however the code to do that when I try to record a macro looks extremely complicated, and I can't figure out where to write "WordApp" at the start to get it to apply that code to my word document
    Here's the code it tells me to write

    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=9, NumColumns:= _
    2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed
    With Selection.Tables(1)
    If .Style <> "Table Grid" Then
    .Style = "Table Grid"
    End If
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = False
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = False
    .ApplyStyleRowBands = True
    .ApplyStyleColumnBands = False
    End With

    Not sure what to do here to get this table created, anyone know how I can do it?
    Thank you for reading this through!!! Also apologies for bad formatting, excelling at 2am is rough stuff

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    if this is what ms word spit out for you:
    Please Login or Register  to view this content.
    really all you should have to do is just qualify all this code with your WordApp application object. does that make sense? I don't work a lot with word, but I would guess lines of code like this would work:
    Please Login or Register  to view this content.
    do you see what I inserted?

  3. #3
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    Nope soz, still not working, says something about error 450

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    well that doesn't help. how about a little more description? got an image? screenshot? upload a file? I'm simply giving you the tools to do it. hopefully I'm providing something of value.

  5. #5
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    Yes, apologies for not providing enough information
    Debugger gives this
    Attachment 691183

    While the error that it throws up says this
    Attachment 691184

    I'm very much a noob at this, never really done any computer science stuff before, so sorry if I seem a bit clueless as to what I should be doing

  6. #6
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    LOL. That's funny. You know what's even funnier? I've only taken one college course in my life and that was an introduction to Java programming and the only thing we ever did for 6 months straight was print text out to the screen. What a waste of money for me that was. And everybody here is a newbie otherwise you wouldn't be posting questions right? :-) so if someone else doesn't respond here I'll take a look when I get back home I'm traveling at the moment. Thank you for uploading the images by the way that helps.

  7. #7
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    Cubic,

    I'm sorry but for some reason I cannot view attachments when they are uploaded they way you uploaded the last 2. I have no idea what the issue is, but I ran into this with someone else today too. Can you let us know what process you are going through to get those links in your post that start with the word ""Attachment""?? If you go through the following process, the images should automatically be embedded in your posts, plus be attached to the post below the posting text box:

    => click "go advanced" below the box where the posting text goes.
    => click the link called ""manage attachments"" below the posting text box.
    => click the ""browse"" button
    => select the file
    => click the ""upload"" button
    => click ""close this window"" at the bottom.
    Attached Images Attached Images

  8. #8
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    I'll have a go putting them in using that method, both attachments should be in there now, one is what the debugger gives me when I activate it, the other is the error message it throws up when I try and run the code
    Attached Images Attached Images

  9. #9
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    when I used ms word to do what you wanted, which is the following, according to what you said:
    Now I want to make it create a table, with 2 columns and 9 rows, with normal black cell borders
    this is the code that it gave me:
    Please Login or Register  to view this content.
    so, in order to make it work, all you have to do is add what I said earlier. the qualifying object to the code. and that object is the word application. like this:
    Please Login or Register  to view this content.
    and then, from the Microsoft documentation which should always work if you follow the instructions, you need this apparently:
    Please Login or Register  to view this content.
    so, the whole thing, which you might like, but you certainly can modify it to your liking, would be this:
    Please Login or Register  to view this content.
    and this is what you need to look at (any many other pages from microsoft. they have oodles of this information. you can learn greatly from these pages!):

    https://docs.microsoft.com/en-us/off...ord.tables.add
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Re: Trying to Create a table with 2 columns and 9 rows in word, using Excel VBA

    Thanks very much for this, it seems like the code itself works well now, the table is coming up, all I need now is to get it to insert the table at the point which is selected in the word document, I need it to be inserted at a certain point in the document, however that point is variable since part of my document relies on user inputted data with a variable number of letters so the document has a variable length, I've been looking through a bunch of range variable things but can't seem to find an adjustment which will perform this task, do you know how I could do that? If that isn't possible, it might help that the point that is selected in the document is the end of the document at the point along the code that the table is being created, so if there's a way to write 'range = end of document' and then create the table at that range that would work too.
    Thanks so much for helping me solve this problem!
    Last edited by Cubictulip; 08-20-2020 at 12:16 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Create Word-Table with Excel-VBA and style it
    By jumbo125 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-03-2020, 03:00 AM
  2. [SOLVED] Excel if new row is created, auto-populate columns in word doc and create new word doc
    By Eduards in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2019, 08:22 AM
  3. create table with rows and columns defined by check boxes
    By djstar83 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-18-2019, 01:55 PM
  4. Create new Word file then Copy table and contents from Excel to that Word file
    By sbv1986 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2018, 09:14 AM
  5. create a table based on values selected in rows and columns
    By hemagala in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-14-2015, 03:46 AM
  6. Macro to Import Column/Rows from Word Table to Excel Columns
    By atom2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-08-2014, 01:57 AM
  7. [SOLVED] CREATE Excel from WORD Table - HELP PLEASE
    By bas in forum Excel General
    Replies: 2
    Last Post: 03-12-2006, 01:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1