0

While coding and extending the functionality of my Sub there are sadly arising some errors - e.g. a runtime error - but it could be any error...

The main topic of my question is that for me it's impossible to open that specific Word document (Test.docx) by hand (clicking on it in the explorer).

I have found one solution but this one is annoying, because I have to restart my computer and this is time consuming... and I hope there exists a more elegant solution that you can share with me.

So many thanks in advance!

Now my code with an provoked error...

Sub GetInfoOutOfWordDocument()

'Init
Dim appWord As Word.Application
Dim document As Word.Document
Dim strFolder As String
Dim strFile As String
Dim MyArray() As Variant ' for arising the error

' Select the word document
strFolder = "C:\Users\"
strFile = Dir(strFolder & "Test.docx", vbNormal)

' Open the word document
Set appWord = CreateObject("Word.Application")
Set document = appWord.Documents.Open( _
    FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=False, Visible:=False)

' Getting the needed information out of your Word document...

' Now the error occurs - e.g. runtime error
MyArray(1)=5
' Problem: The above opened Word document isn't closed properly and therefore 
I'm not able to open the specific Word document by hand

dokument.Close wdDoNotSaveChanges
appWord.Quit
Set document = Nothing
Set appWord = Nothing

End Sub
tueftla
  • 369
  • 1
  • 3
  • 16
  • How about implementing an error handling, which closes the document if an error occurs? – Shrotter Oct 31 '22 at 08:21
  • @Shrotter I think then I must implement an error handling to every line of code. Applicable for few lines but not for many lines of code – tueftla Oct 31 '22 at 08:24
  • 1
    Please check the possibilities of _on error goto_ (e.g. [here](https://stackoverflow.com/a/24187560/17172829) in finally or catch you would implement the closing of the document) – Shrotter Oct 31 '22 at 08:36
  • You need to dimension myArray using redim before you can assign values to members of myArray. Just doing 'Dim myArray as Variant' creates the safearray structure but doesn't allocate any memory. – freeflow Oct 31 '22 at 08:38
  • Your filename may also be incorrect. It seems to me that your path will be 'C:Users\...\Test.docx' but '...' isn't a legal folder name. – freeflow Oct 31 '22 at 08:41
  • @Shrotter Is it possible to write `On Error GoTo Finally` in the **first line** of my sub and then doing my coding with hundreds of code lines?! In `Finally` at the end of my code there will be the "word-closing-procedure". Btw. awesome suggestion (the link)! – tueftla Oct 31 '22 at 09:00
  • @freeflow Indeed, these are two errors in my code. But my issue is, that I'm doing flaw in my code, which I'm not aware of - in this code example `MyArray(1)=5` without the redim line (`Redim Preserve MyArray(1)`). This error is done extra that you can see *my issue* of not being able to open the word document via the explorer because my sub failed and therefore the proper "word-closing-procedure" wasn't done. – tueftla Oct 31 '22 at 09:10
  • 1
    There are errors you control because you own the code. In this code you should do appropriate tests to ensure that the errors do not occur so errors of the genre myArray(1)=5 example is solveable. There are errors you do not control, such as trying to open a non existant file. In such cases you should encapsulate the line(s) of code in a try function so that you bring the scope of the error back to errors you control. Having done both of the above the problem you describe will go way. – freeflow Oct 31 '22 at 09:44
  • 1
    `appWord.Visible = True` would leave Word visible in the event an error occurred, and you'd be easily able to close any open document... – Tim Williams Oct 31 '22 at 15:36

0 Answers0