Opening Word document via Excel VBA not working

richh

Board Regular
Joined
Jun 24, 2007
Messages
245
Office Version
  1. 365
  2. 2016
I wrote a piece a code last week that was working fine this morning but now doesn't work. No idea why. upon selecting an item from a listbox, the onClick function of that control sends the value selected to this founction. It should then find the topic selected in the list of topics, then create a wird application, open the document, pull a small bit of data out, close the doc, then quit the app. Now it gets hung up when it attempts to open the file.

VBA Code:
Public Function PopulateSelectedPreview(topicSelected As String)
    Dim ws      As Worksheet
    Dim lRow    As Long
    Dim pathStr As String
    Dim wordapp As Object, objNewDoc As Object ''Word.Document
        
    Set ws = Application.ThisWorkbook.Worksheets("ResponseData")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 2 To lRow
        If ws.Cells(i, 4) = topicSelected Then
            pathStr = ws.Cells(i, 7)
            Set wordapp = CreateObject("Word.Application")
            wordapp.Documents.Open (pathStr) '<--------------error here
            'wordapp.Visible = False
                        
            UserForm1.selectedPreview.Text = wordapp.activedocument.selectcontentcontrolsbytitle("Response Body").Item(1).Range.Text
            wordapp.Documents.Close
            wordapp.Quit
                        
            Exit For
        End If
    Next i
    
    Set wordapp = Nothing
    Set ws = Nothing
End Function
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You said it gets hung up, and you said there is an error. Can you be specific about what happens on that line of code? Do you get an error message? What does it say?

What is the value of pathStr at the point where the error occurs? Have you verified that it is a valid path, and the file exists?
 
Upvote 0
I think I figured it out, but not sure how to address it. I had ran the code several times and the document was not closed. Icouldn't figure out out - I guess opening a word app instance happens in the background..? I couldn't see the file so I assumed it wasn't opening.

I had to force close Excel to get it to stop trying to open the document and in turn, had started up several instances of Word in the background. After forcing them all closed, I tried to open the doc directly and Word (Office? Windows?) had flagged it as having encountered an error the last time it was opened, which had prevented it from being opened normally. I then had to re-save the file to remove that flag.

Now I have to figure out how to check to see if the document is open before attempting to open it. And thus, another 50 lines of code need to be developed to address one error.... wa wa waaaa
 
Upvote 0
Try to replace this code

VBA Code:
            Set wordapp = CreateObject("Word.Application")

with this code

VBA Code:
On Error Resume Next
Set wordapp = GetObject(, "Word.Application")
If wordapp Is Nothing Then Set wordapp = CreateObject("Word.Application")
On Error GoTo 0

This will use an existing instance of Word if one is already open, which I think is probably the issue. Also, if you do this, consider if you really want to Quit an instance that was already open when you started.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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