Solved

VBA Word Array, to Excel file

Posted on 2011-02-14
6
582 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

758 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