-1

I already have a Macro in Excel that pulls through data from specific tables, rows and columns in a specified Word doc and returns it to cells in my Excel s/sheet. I need to make 2 alterations to the code but my knowledge is not advanced enough.

  1. I need to run this code on multiple Word docs in a specified folder, whether it is .doc or a .docx

  2. I need to establish why on some Word docs, the code fails to pull through the data from the Word doc and I get RUN TIME ERROR CODE '4605' 'The method or property is not available because no text is selected'. I tried putting, 'on error resume next', at the start of the module so it keeps on running to the end, in the hope that some text would get pulled through, but still none of the cells in my Excel s/sheet get populated.

Sub ImportFromWord()

On Error Resume Next

  'Activate Word Object Library

  Dim WordDoc As Word.Document

  Set WordApp = CreateObject("word.application") ' Open Word session

  WordApp.Visible = False 'keep word invisible
  Set WordDoc = WordApp.Documents.Open("C:\Users\brendan.ramsey\OneDrive - Ofcom\Objectives\Brendan's Objectives 2022-23\Licence calls\test 2.docx") ' open Word file

  'copy third row of first Word table
  WordDoc.Tables(1).Cell(Row:=1, Column:=3).Range.Copy

  'paste in Excel
  Range("A3").PasteSpecial xlPasteValues
  
  WordDoc.Tables(4).Cell(Row:=3, Column:=6).Range.Copy
  Range("B3").PasteSpecial xlPasteValues
  
  WordDoc.Tables(4).Cell(Row:=3, Column:=3).Range.Copy
  Range("C3").PasteSpecial xlPasteValues
  
  WordDoc.Tables(5).Cell(Row:=2, Column:=5).Range.Copy
  Range("D3").PasteSpecial xlPasteValues
  
  WordDoc.Tables(5).Cell(Row:=2, Column:=7).Range.Copy
  Range("E3").PasteSpecial xlPasteValues
  
  WordDoc.Tables(5).Cell(Row:=2, Column:=2).Range.Copy
  Range("F3").PasteSpecial xlPasteValues



  WordDoc.Close 'close Word doc
  WordApp.Quit ' close Word

End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • See, for example: https://forums.excelguru.ca/threads/help-with-vba-to-extract-data-from-word-to-excel.8900/post-36586 & https://forums.excelguru.ca/threads/help-with-vba-to-extract-data-from-word-to-excel.8900/post-36594 – macropod Nov 07 '22 at 21:49
  • Do you now how I can adapt the code to search for .doc and .docx files and run the code through multiple Word files in a specified FOLDER and output the data from each Word doc onto each seperate rows of my spreadsheet please? – Brendan Ramsey Nov 08 '22 at 09:48
  • The code already processes .doc, .docx and .docm files. Modifying the code to output the data from each Word doc onto each separate rows on the same sheet is a trivial undertaking. – macropod Nov 08 '22 at 20:54

2 Answers2

0

RUN TIME ERROR CODE '4605' 'The method or property is not available because no text is selected'.

Runtime Code 4605 happens when Microsoft Word fails or crashes whilst it's running. It doesn't necessarily mean that the code was corrupt in some way, but just that it did not work during its run-time. This kind of error will appear as an annoying notification on your screen unless handled and corrected. Here are symptoms, causes and ways to troubleshoot the problem.

As the error message says there is no text selected. To find out what property or method gives the error message I'd recommend breaking the chain of calls in the single line of code by declaring each property or method call on a separate line, so you will know which call fails exactly.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

Your code may behave better if you avoid all that copy/paste and transfer the cell contents directly:

Sub ImportFromWord()
    Const FLDR_PATH As String = "C:\Temp\Docs\"
    Dim WordDoc As Word.Document, WordApp As Word.Application
    Dim rw As Range, f
    
    Set rw = ActiveSheet.Rows(3) 'or some other sheet

    f = Dir(FLDR_PATH & "*.doc*") 'check for document
    Do While Len(f) > 0
        
        If WordApp Is Nothing Then 'open word if not already open
            Set WordApp = CreateObject("word.application")
            WordApp.Visible = False
        End If
        
        With WordApp.Documents.Open(FLDR_PATH & f, ReadOnly:=True) ' open Word file
        
            WordCellToExcel .Tables(1).Cell(Row:=1, Column:=3), rw.Cells(1)
            WordCellToExcel .Tables(4).Cell(Row:=3, Column:=6), rw.Cells(2)
            WordCellToExcel .Tables(4).Cell(Row:=3, Column:=3), rw.Cells(3)
            'etc etc
            .Close savechanges:=False
        End With
        
        Set rw = rw.Offset(1) 'next row down
        f = Dir()             'next file, if any
    Loop
    
    If Not WordApp Is Nothing Then WordApp.Quit ' close Word if it was opened

End Sub

'transfer content from a cell in a Word Table to an Excel range
Sub WordCellToExcel(wdCell As Word.Cell, destCell As Range)
    Dim v
    v = wdCell.Range.Text
    destCell.Value = Left(v, Len(v) - 2) 'remove "end of cell" marker
End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Thanks Tim, works a treat :) Do you now how I can adapt the code to search for .doc and .docx files and run the code through multiple Word files in a specified FOLDER and output the data from each Word doc onto each seperate rows of my spreadsheet please? – Brendan Ramsey Nov 08 '22 at 09:48
  • See edits above for looping over a folder. – Tim Williams Nov 08 '22 at 16:35
  • Thanks Tim will this work for .docx files as well? – Brendan Ramsey Nov 08 '22 at 16:39
  • "*.doc*" matches both `.doc` and `.docx` – Tim Williams Nov 08 '22 at 16:44
  • Just tried the code and changed the Const FLDR_PATH As String = "C:\Temp\Docs\" to the path where my Word docs are stored but nothing is happening. Like the code doesn't even run? – Brendan Ramsey Nov 09 '22 at 14:45
  • Did you try stepping through it? – Tim Williams Nov 09 '22 at 15:53
  • It worked perfectly when I specified a particular Word document in your last edition of code. I have used the latest code and modified the path, as above. How do I step through it? It just doesn't run for some reason. – Brendan Ramsey Nov 09 '22 at 15:57
  • https://www.wiseowl.co.uk/blog/s196/step-through-code.htm – Tim Williams Nov 09 '22 at 15:59
  • F8 highlights `Sub ImportFromWord()` then F8 again highlights `Set rw = ActiveSheet.Rows(3) 'or some other sheet` then F8 again highlights `f = Dir(FLDR_PATH & "*.doc*") 'check for document` then F8 again highlights 'Do While Len(f) > 0' then F8 again highlights `If Not WordApp Is Nothing Then` and finally F8 again highlights `End Sub`. – Brendan Ramsey Nov 09 '22 at 16:11
  • So it never gets to `Set WordApp = CreateObject("word.application")` ? Check Task Manager for hung Word instances, and maybe change that `WordApp.Visible = False` to `WordApp.Visible = True` while troubleshooting, so you can see what's going on. – Tim Williams Nov 09 '22 at 16:27
  • Just tested, and it's working OK for me. – Tim Williams Nov 09 '22 at 16:52
  • I checked Task Manager and there were no hung Word instances. Also tried changing `WordApp.Visible = False` to `WordApp.Visible = True` but made no difference. I am lost to be honest, – Brendan Ramsey Nov 09 '22 at 17:30
  • This is the only command line I am editing to add the path `Const FLDR_PATH As String = "C:\Users\brendan.ramsey\OneDrive - Ofcom\Objectives\Brendan's Objectives 2022-23\Licence calls"` – Brendan Ramsey Nov 09 '22 at 17:32
  • Have just stuck the original code back in and all works fine on just one specified Word doc. – Brendan Ramsey Nov 09 '22 at 17:38
  • Did you include the \ on the end of `FLDR_PATH` ? – Tim Williams Nov 09 '22 at 17:39
  • No I didn't but I have now and it works a treat. Thank you so much for all your kind help :) – Brendan Ramsey Nov 09 '22 at 20:20