Results 1 to 2 of 2

Excel VBA to reference open word document

Threaded View

  1. #1
    Registered User
    Join Date
    09-17-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    5

    Excel VBA to reference open word document

    Hi....I have code which copies table information from word in to an excel spread sheet....see below....with the current script you have to select the word document but I would like it to pull the information from any word document that is open (this is opened from an e-mail attachment and there will only ever be on word document open at a time). The excel spread sheet where the data is copied to will always be open. How easy or not is this to do? The other alternative could be I that the word document is saved to a temporary file which this script calls.

    Sub ImportWordTabletest()
    'Import all tables to a single sheet
    Dim wdDoc As Object
    Dim wdFileName As Variant
    Dim TableNo As Integer 'table number in Word
    Dim iRow As Long 'row index in Word
    Dim jRow As Long 'row index in Excel
    Dim iCol As Integer 'column index in Excel
    wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
    "Browse for file containing table to be imported")
    If wdFileName = False Then Exit Sub '(user cancelled import file browser)
    Set wdDoc = GetObject(wdFileName) 'open Word file
    With wdDoc
        If wdDoc.tables.Count = 0 Then
            MsgBox "This document contains no tables", _
                vbExclamation, "Import Word Table"
        Else
            jRow = 0
            For TableNo = 1 To wdDoc.tables.Count
                With .tables(TableNo)
    'copy cell contents from Word table cells to Excel cells
                    For iRow = 1 To .Rows.Count
                        jRow = jRow + 1
                        For iCol = 1 To .Columns.Count
                            On Error Resume Next
                            ActiveSheet.Cells(jRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                            On Error GoTo 0
                        Next iCol
                    Next iRow
                End With
                jRow = jRow + 1
            Next TableNo
        End If
    End With
    Set wdDoc = Nothing
    End Sub
    Some guidance would be much appreciated

    Rgds



    Sharon
    Last edited by arlu1201; 12-21-2012 at 01:07 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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