Importing word docs leaves tons of open processes

daneyuleb

New Member
Joined
Dec 8, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hoping someone can point me in a direction--tearing my hair out! (and I don't have much!)

My users can call a function to import specific word file's text into a shapes textbox. Works fine.
But, after each call, they have a "Microsoft Word (32 bit) process running in the Windows task manager--even though I "try" to close and quit the word app I started. They can end up with dozens or hundreds--one for each import operation they've called.

Worse--if they force quit all the processes--the next call to import a doc will usually fail (doesn't give an error--just... doesn't happen). In fact, I have to remove the "docApp.Quit" line shown below as it does the same thing--with it in place they get one good import, then... nothing!

Is there some better, more reliable way to deal with these Word operations that doesn't leave orphaned processes? Here's the stripped down code I'm using to do the imports...


VBA Code:
Function ImportWordDoc()
    
Dim docApp As Object
Dim docMyDoc As Object
Dim Doc_to_import As String

    Set docApp = CreateObject("Word.Application")
    Set docMyDoc = CreateObject("Word.document")
    Set docMyDoc = Documents.Open("my_word_file.docx", Visible = True)

    ' Get our Word data (grabbing all of it) into the clipboard then set our textbox to the clipboard contents
    With docMyDoc
      .Range(Start:=0, End:=.Characters.count).Copy
      Ticket_sheet.Shapes("Textbox_1").TextFrame.Characters.Text = Clipboard     
    End With

    ' Close the Word doc, get rid of objects...
    docMyDoc.Close
    docApp.Quit       '<------ I can't leave this in, it makes the next import fail
    Set docMyDoc = Nothing
    Set docApp = Nothing

End Function
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Here's the stripped down code
I'm guessing that means that you removed a load of code? If so, that's not going to help us to help you. The most likely cause of this kind of issue is an object reference that isn't properly qualified so we need to see all of the actual code. Quitting the application you started is necessary and should not prevent you starting a new instance.

As an aside, this line serves no purpose that I can see:

Code:
Set docMyDoc = CreateObject("Word.document")

and should be removed. (it might even be the root of your problem).
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top