Not able to open Word documents using a Word macro

musicgold

Board Regular
Joined
Jan 9, 2008
Messages
197
Hello,

I am trying to open certain Word documents using the macro below, but it doesn't seem to be working. When I run the macro, I am not getting any error.

In the selected area, each line has a network drive link to a word file in the following format: K:\Sales\John\Sept\Notes\J2021.docx

Why is the macro not working?

VBA Code:
Dim openfile As Document
Dim singleLine As Paragraph
Dim lineText As String

    For Each singleLine In Selection.Paragraphs
        lineText = singleLine.Range.Text
   Set openfile = Word.Documents.Open(lineText)
     
    Next singleLine
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
It should really be:

Code:
Set openfile = Application.Documents.Open(lineText)
 
Upvote 0
It should really be:

Code:
Set openfile = Application.Documents.Open(lineText)
That is also not working. As per the Watch window, value of openfile remain "nothing". No error occurs during the execution of the code.
Thanks
 
Upvote 0
Is that line actually being executed?
I single-stepped the macro on two computer. On my work computer, the control just executes the line and triggers no error. On my home computer, I get the error below. The file is there.

1637848162859.png
 
Upvote 0
Are you sure the path is exactly correct? That message is pretty self-explanatory. ;)
I think so. I even tried with really simple paths, but continue to get the same message. As you can see in the second screenshot. the file exists.

1637859357336.png

1637859472253.png
 
Upvote 0
Ahh, the clue is in the fact that the closing bracket in the message is on a new line. The line text includes a carriage return at the end, so you'll need to remove that - either use Replace or take everything up to the last character of the line.
 
Upvote 0
Ahh, the clue is in the fact that the closing bracket in the message is on a new line. The line text includes a carriage return at the end, so you'll need to remove that - either use Replace or take everything up to the last character of the line.
Bingo! The line below solved the problem. Thank you very much!?

lineText = Replace(lineText, vbCr, "")
 
Upvote 0

Forum statistics

Threads
1,223,986
Messages
6,175,788
Members
452,670
Latest member
nogarth

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