Code runs in break mode, but not otherwise!

ExTeacher

New Member
Joined
Mar 2, 2017
Messages
6
Hi all,

I have a code that is replacing hyperlinks to open word documents on a networked document as everyone has a unique identifier for the initial drive on the network. For example, I am on the H drive, but the same document for a colleague is on the N drive. I've created a range in excel called filepath, using the =cell("filename") code so each person will have their drive registered to the document.

It runs in break mode, but I receive the 4198 error when I try and run it as normal. I have about 80 documents I need to hyperlink to a master - if anyone has any alternative methods please let me know!

The code is:

Code:
 Sub label1_Click()
    Dim objWord As Object
    Dim objDoc 

    
    Set objWord = CreateObject("Word.Application")
    
    x = Range("filepath") & "\Stage 3.7 Coordinator Item Check.docx"
    
    objWord.Documents.Open (x)
    
    objWord.Visible = True
    
End Sub

Thanks!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Welcome to the forum.

It doesn't look like you need to control Word after opening the file, so perhaps try this. Add the following to the top of the module:
Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                                      (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
                                       ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOW            As Long = 5&
Sub OpenFile(strFilePath As String)
   ShellExecute Application.hWnd, "Open", strFilePath, 0&, 0&, SW_SHOW
End Sub

then change your click code to this:

Code:
Sub label1_Click()

    OpenFile Range("filepath") & "\Stage 3.7 Coordinator Item Check.docx"
    
End Sub
 
Upvote 0
Works perfectly. Thank you. Would it be possible to explain what the code is doing? I like knowing these things so next time I can try and figure it out for myself :)

Thanks again!
 
Upvote 0
The ShellExecute API is a hook into the operating system functions. This is essentially the code equivalent of you finding the docx file in Explorer and double-clicking it.
 
Upvote 0
Rory has already provided an alternative, so just FWIW...

objWord.Documents.Open (x)

IMO, there should not be parenthesis around the x, you are not returning anything.

Mark
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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