richh
Board Regular
- Joined
- Jun 24, 2007
- Messages
- 245
- Office Version
- 365
- 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