Solved

Running a VBA macro in Word 2010 to write to an Excel 2010 worksheet

Posted on 2013-11-08
1
811 Views
Last Modified: 2013-11-10
Hi,

I have a macro-enabled word 2010 document.  I am trying to write to an Excel 2010 worksheet from this word doc.  

What declaration are needed in the module in word 2010 for EXCEL to write to a excel 2010 worksheet?

I can write to an excel file by ANY name (don't care what it is) that will contain my destination worksheet

    Dim obWord As Object
    Dim doc As Word.Document
    Dim docTable As Word.Table
    Dim tblRow As Long, tblCol As Long

    'Something is MISSING HERE
    'Lines are needed here to write and excel file that contains the worksheet declared below
    'Please remember that this will be running in  Word 2010
    Dim ws As Worksheet
0
Comment
Question by:jmohsin
1 Comment
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
Here is some code to write to a worksheet from Word. Note that it uses Early binding, so you need to have a reference to the Microsoft Excel Object Library:
Sub xl()
    Dim xlApp As Excel.Application
    Dim xlWbk As Excel.Workbook
    Dim xlWks As Excel.Worksheet
    
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlWbk = xlApp.Workbooks.Add
    Set xlWks = xlWbk.Worksheets(1)
    xlWks.Cells(1, 1).Value = "XXX"

End Sub

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now