Solved

VBA Word Array, to Excel file

Posted on 2011-02-14
6
584 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

947 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

20 Experts available now in Live!

Get 1:1 Help Now