Solved

VBA Word Array, to Excel file

Posted on 2011-02-14
6
589 Views
Last Modified: 2012-05-11
VBA Word Array, to Excel file
Hi guys lets say I have an array in word VBA of type:

MyArr ( Row, Column )

(Where Row and Column are Excel’s Sheet Row and Column)

I just need to export it to Excel (or excel file format) from a word VBA macro.

Good Points are given for this question....

Take care
0
Comment
Question by:New_Alex
  • 3
  • 3
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34887860
You will need something like this.
Sub ArrayExcel()
    Dim xlApp As Excel.Application
    Dim xlWbk As Excel.Workbook
    Dim xlWks As Excel.Worksheet
    Dim r As Integer
    Dim c As Integer
    
    Set xlApp = New Excel.Application
    Set xlWbk = xlApp.Workbooks.Add
    xlApp.Visible = True
    Set xlWks = xlWbk.Sheets(1)
    For Each ffld In ActiveDocument.FormFields
        xlWks.Cells(r, c).Value = MyArr(r, c)
        c = c + 1
    Next ffld
    'xlWbk.SaveAs ....
    'xlWbk.Close
    'xlApp.Quit
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34887881
Oops. That was the unfinished version that I copied by mistake
Sub ArrayExcel2()
    Dim xlApp As Excel.Application
    Dim xlWbk As Excel.Workbook
    Dim xlWks As Excel.Worksheet
    Dim r As Integer
    Dim c As Integer
    
    Set xlApp = New Excel.Application
    Set xlWbk = xlApp.Workbooks.Add
    xlApp.Visible = True
    Set xlWks = xlWbk.Sheets(1)
    For r = 0 To UBound(MyArr, 1)
        For c = 0 To UBound(MyArr, 2)
            xlWks.Cells(r + 1, c + 1).Value = MyArr(r, c)
        Next c
    Next r
    
    'xlWbk.SaveAs ....
    'xlWbk.Close
    'xlApp.Quit
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:New_Alex
ID: 34894241
Hey,

Like said, I would like to have that run in Microsoft Word VBA environment. Because the array comes from word document.

Any way to make the above code work in MS word VBA?

Thanks Friends

Love !
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:New_Alex
ID: 34903949
Hi Guys, at least if I manage to transfer the variable to Excel VBA and then run an Excel VBA Macro?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 34904563
The code is written in Word VBA for running there.
I did forget to mention that you will need a set a reference (Tools/References in the Word VBA editor) to the Microsoft Excel Object Library.

It would run in Excel, but this very slightly tweaked version uses current Workbook instead of creating a new one.
'Dim xlApp As Excel.Application
    'Dim xlWbk As Excel.Workbook
    Dim xlWks As Excel.Worksheet
    Dim r As Integer
    Dim c As Integer
    
    'Set xlApp = Application New Excel.Application
    Set xlWbk = ActiveWorkbook
    'xlApp.Visible = True
    Set xlWks = xlWbk.Sheets(1)
    For r = 0 To UBound(MyArr, 1)
        For c = 0 To UBound(MyArr, 2)
            xlWks.Cells(r + 1, c + 1).Value = MyArr(r, c)
        Next c
    Next r
    
    'xlWbk.SaveAs ....
    'xlWbk.Close
    'xlApp.Quit

Open in new window

0
 
LVL 1

Author Closing Comment

by:New_Alex
ID: 35033989
Even if it answers the question, it seems that there are not any better answers than this. The solution needs excel to run and it does not export directly to file. It also needs extra reference settings.

I prefer to export in .xml format  !
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

828 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