trouble fixing "object invoked has disconnected from its clients" error

Joined
May 16, 2012
Messages
3
Reaction score
0
[SOLVED] trouble fixing "object invoked has disconnected from its clients" error

Hello

First time writing a macro in word, here. I have a simple command button in my word doc which should bring up a userform which the user populates with information, etc. etc. Unfortunately, I'm getting the "object invoked has disconnected from its clients" error, which has a summary here: h t t p : / / support.microsoft.com/kb/319832

I've been googling this error for a couple hours now, and I guess I'm not understanding the suggested fixes, as they aren't working for me. Can anyone help me understand how to fix this? I thought that with the Dim/definition of my oWd variable I would be fixing the error, but the error persists.

I'll also note this weird thing: when I enter developer mode and double click on the button to call up the macro code, it comes up just fine. However, the very first time I hit F8, the word application automatically puts itself "on top" over the vba editor, but doesn't pop up the form. I have to manually click back into the vba editor, then hit F8 again, for the debugger to start actually stepping through the code.

The code is working just fine, the only problem is that once it's finished running, I can't edit, close, etc. the document because of the client disconnection error.

code for the command button:
Code:
Private Sub CommandButton1_Click()
    UfNewSlice.Show
End Sub

code for the form:

Code:
Private Sub UfNewSlice_Initialize()
vbEntry.Value = "Enter product description here"
vbPubtag.Value = "Enter Pubtag here"
End Sub


Private Sub vbOK_Click()
On Error GoTo Err_Execute
Dim SliceEntry As String, PubTagEntry As String
SliceEntry = vbEntry.Value

If ActiveDocument.Bookmarks.Exists(PubTagEntry) = True Then
    PubTagEntry = (vbPubtag.Value & "1")
Else
    PubTagEntry = vbPubtag.Value
End If

Call InsertNewSliceEntry(SliceEntry, PubTagEntry)

Unload Me
    Exit Sub
Err_Execute:
        MsgBox "An error occurred."
End Sub



Private Sub vbCancel_Click()
    Unload Me
End Sub
Private Sub vbClear_Click()
    Call UfNewSlice_Initialize
End Sub



Private Function InsertNewSliceEntry(SliceEntry As String, PubTagEntry As String)
Selection.GoTo what:=wdGoToBookmark, Name:="CuSlSectionEnd"
Dim bmNewSlice As Range, oWd As Word.Application
Set oWd = Word.Application
oWd.Visible = True
Set bmNewSlice = oWd.ActiveDocument.Bookmarks("newSlice").Range
    bmNewSlice.Text = SliceEntry


oWd.ActiveDocument.Bookmarks.Add Name:=PubTagEntry, Range:=bmNewSlice
oWd.Selection.GoTo what:=wdGoToBookmark, Name:=PubTagEntry
        oWd.Selection.Font.Bold = wdToggle
        oWd.Selection.Font.Color = wdColorRed
        oWd.Selection.Font.Size = 16
        oWd.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
oWd.Selection.MoveDown Unit:=wdLine, Count:=1
oWd.Selection.TypeText Text:=(vbLf & vbLf & vbLf & vbLf)
oWd.Selection.MoveUp Unit:=wdLine, Count:=1
oWd.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
oWd.ActiveDocument.Bookmarks.Add Name:="newSlice"


oWd.Selection.GoTo what:=wdGoToBookmark, Name:="CuSlTOCEnd"
oWd.Selection.MoveLeft Unit:=wdCharacter, Count:=1
oWd.Selection.TypeText Text:=(vbLf & SliceEntry)
oWd.Selection.MoveUp Unit:=wdLine, Count:=1
oWd.Selection.Find.ClearFormatting
    With oWd.Selection.Find
        .Text = SliceEntry
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
    End With
    oWd.Selection.Find.Execute
oWd.ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
    SubAddress:=PubTagEntry, ScreenTip:="", TextToDisplay:=SliceEntry
  Exit Function
End Function
 
Last edited:
Joined
May 16, 2012
Messages
3
Reaction score
0
I figured this error out and wanted to leave the solution here for others to find in the future, if they're getting it for the same reason that I was.

In Word 2007, the Hyperlinks object is looking for an Object as its Anchor. For this reason, setting "selection.range" as the Anchor will cause the code to break, disconnect from the client, and usually cause word to crash as well.

The solution is to Dim something as an Object, set it equal to Selection.Range, and then set the Anchor equal to that variable. For example:

After selecting the text which you wish to be hyperlinked (I did this through a basic "Find" but you can do this any way you wish):

Old, causes error:

Code:
ActiveDocument.Hyperlinks.Add [COLOR="Red"]Anchor:=Selection.Range[/COLOR], Address:="", _
    SubAddress:=PubTagEntry, ScreenTip:="", TextToDisplay:=SliceEntry

Fixed:

Code:
[COLOR="YellowGreen"]Dim CuSlTocEntry As Object
Set CuSlTocEntry = Selection.Range[/COLOR]
ActiveDocument.Hyperlinks.Add [COLOR="YellowGreen"]Anchor:=CuSlTocEntry[/COLOR], Address:="", _
    SubAddress:=PubTagEntry, ScreenTip:="", TextToDisplay:=SliceEntry

In my code, there was no need to qualify any Global Object references. Calls to the "ActiveDocument," "Selection," "Hyperlinks" and "Bookmarks, " etc., Objects do not need to be qualified in Word 2007 because they are all native to Word VBA.

If I were to write a macro that, for example, started in a Word doc but then created an Excel document and worked in that as well, I might want to qualify my Global Object references, which means specifying whether you're trying to reference the "Word.Application" or "Excel.Application" each time you reference a Global Object.

Again though, my macro resides entirely in Word and all its Global Object references are native to the Word environment, so I do not need to qualify these Object references.

My final code looks like this:

Code:
Private Function InsertNewSliceEntry(SliceEntry As String, PubTagEntry As String, FilledCuSl As String)
Selection.GoTo what:=wdGoToBookmark, Name:="CuSlSectionEnd"
Dim bmNewSlice As Range

Set bmNewSlice = ActiveDocument.Bookmarks("newSlice").Range
    bmNewSlice.Text = SliceEntry


ActiveDocument.Bookmarks.Add Name:=PubTagEntry, Range:=bmNewSlice
Selection.GoTo what:=wdGoToBookmark, Name:=PubTagEntry
        Selection.Font.Bold = wdToggle
        Selection.Font.Color = wdColorRed
        Selection.Font.Size = 16
        Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:=(vbLf & vbLf & vbLf & vbLf)
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
ActiveDocument.Bookmarks.Add Name:="newSlice"

'Selection.MoveUp Unit:=wdLine, Count:=2
'Dim wdApp As Object, wdDoc As Object
    'Set wdApp = CreateObject("Word.Application")
    'Set wdDoc = wdApp.Documents.Open(FilledCuSl)
    
    'FilledCuSl

Selection.GoTo what:=wdGoToBookmark, Name:="CuSlTOCEnd"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=(vbLf & SliceEntry)
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Find.ClearFormatting
    With Selection.Find
        .Text = SliceEntry
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
    End With
    Selection.Find.Execute

[COLOR="YellowGreen"]Dim CuSlTocEntry As Object
Set CuSlTocEntry = Selection.Range

ActiveDocument.Hyperlinks.Add Anchor:=CuSlTocEntry, Address:="", _
    SubAddress:=PubTagEntry, ScreenTip:=""[/COLOR]  
Exit Function
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top