+ Reply to Thread
Results 1 to 4 of 4

excel vba to create MS Word document

  1. #1
    Registered User
    Join Date
    02-19-2017
    Location
    Adelaide, Australia
    MS-Off Ver
    16
    Posts
    19

    excel vba to create MS Word document

    Hello, I'm using Excel VBA code to create a MS Word document, based on a Word template.

    I created table using Set objTable = wDoc.Tables(1). All works fine, but I need to position this table in specific spot within my document.
    So I created a label {table1_here} in Word template. I find this label using wDoc.Application.Selection.Find.Text = "{table1_here}", but it seems that wDoc.Application.Selection = "ABC" can only replace label with a string, and I cannot do for example wDoc.Application.Selection = objTable.

    So in my Word template I have;
    line 1
    line 2
    {table1_here} (I want whole table to go here)
    line 10
    etc

    How can I do this? Any help is much appreciated.


    Dim wApp As Object 'Word.Application
    Dim wDoc As Object 'Word.Document
    Dim objRange As Object
    Dim objTable As Object
    Dim wdFormatXMLDocument As Integer

    Set wApp = CreateObject("Word.Application")
    wApp.Visible = False
    wApp.DisplayAlerts = False
    Set wDoc = wApp.Documents.Open(Filename:="word template.dotx", ReadOnly:=True)

    wDoc.Application.Selection.Find.Text = "{table1_here}"
    wDoc.Application.Selection.Find.Execute

    Set objRange = wDoc.Range
    wDoc.Tables.Add objRange, 20, 10
    Set objTable = wDoc.Tables(1)

    'wDoc.Application.Selection = "ABC" '<----------------------
    wDoc.Application.Selection = objTable '<-----------------------

    wDoc.Application.Selection.EndOf

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: excel vba to create MS Word document

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    02-19-2017
    Location
    Adelaide, Australia
    MS-Off Ver
    16
    Posts
    19

    Re: excel vba to create MS Word document

    Hi there and thanks for a quick post.
    It almost works, I mean it finds the label, and replaces it with the table. But it doesn't just replace the label - it also replaces the whole document - I mean pre and post label lines are also overwritten.
    I guess i could do something like - Set objRange = wDoc.Paragraphs(9).Range, and this will put table straight into paragraph #9
    Cheers

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: excel vba to create MS Word document

    It worked for me. Not sure what to tell you.

    Alternatively, you could insert a bookmark and use that.

    In this example, the bookmark is called MyBookmark

    Please Login or Register  to view this content.

+ 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. How to create a word document from excel vba 2007
    By valgo54 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2013, 09:32 AM
  2. Create word document from data entered in excel
    By smrutikanta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-04-2013, 02:50 PM
  3. Add text to Existing Word Document if it Doesn't exist create Word Document
    By unstable81 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-10-2013, 12:15 AM
  4. How to use an excel database to create a word document.
    By viveksharma in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-03-2013, 07:14 PM
  5. how do I create a word document with excel in it
    By szcoder in forum Excel General
    Replies: 0
    Last Post: 04-13-2012, 03:25 PM
  6. VBA code from Excel to create and add info into a Word Document
    By Boitumelo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-07-2011, 05:15 AM
  7. Create a Word document using excel
    By knapper78 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2010, 05:40 PM
  8. Create Word Document from Excel
    By Arun in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-22-2005, 04:05 PM

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