Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Label controls stopped working

Status
Not open for further replies.

bdurst

Programmer
May 17, 2007
8
US
I created an MS Word template which has a couple of labels called "lblSite" and "lblStatus" on it. When the user clicks on these labels, the click event is fired and some code that I wrote is executed.

Everything was working fine until I added the following line to create a label on the fly:
Set ctrlLabel = Selection.InlineShapes.AddOLEControl(ClassType:="Forms.Label.1", Range:=Selection.Range)

After this line is executed, the click events for "lblSite" and "lblStatus" no longer fire when I click those labels. However, if I exit the word document and then reopen it everything works fine.

Do I need to do some kind of clean up after using AddOLEControl? If not, does anyone have an idea as to what could be happening?

 
The line of code is in the Document_New() procedure of the ThisDocument object for the template.

The selection is the current position of the cursor (somewhere in the middle of document).
 
No, I don't have any specific reason for using selection as opposed to activeDocument.

I did try using activeDocument after reading your post to see if anything changed but it did not.

 
The line of code is in the Document_New() procedure of the ThisDocument object for the template.

The selection is the current position of the cursor (somewhere in the middle of document).
So there is other code in Document_New that moves the Selection?

Otherwise, the Selection (coming out of Document_New) will be at the start of the document.

What is this code?
When the user clicks on these labels, the click event is fired and some code that I wrote is executed.
What is this code?

I am grasping at straws here I admit. I can not duplicate this so far.

Gerry
My paintings and sculpture
 
There is just one line of code that is supposed to fire when the labels are clicked: "formName.show". If I put a breakpoint on this line of code and then click on the label, the breakpoint is never reached.

I also tried to recreate this error on a very simple test document but was unable to. So I guess that means the problem is related to the code. Here's the code in the document_New event:



processClarificationOrder = False

'default status OR COULD PULL FROM DATABASE!!!!!!!!!!!!!!!!!
ActiveDocument.lblStatus = "PHYSICAL THERAPY STATUS REPORT - Initial Evaluation"

'set default site. Pull from registry
Dim defaultSite

defaultSite = GetSetting(appname:="PT_Trans", Section:="appInfo", _
Key:="defaultSite") ', setting:=cmbSite)

'fill out address info
On Error Resume Next
ActiveDocument.lblSite = defaultSite & " " & GetSetting(appname:="PT_Trans", Section:="sites", _
Key:=defaultSite, Default:="No Address Found")
On Error GoTo 0

Dim Temp, defaultPhone

On Error Resume Next
defaultPhone = GetSetting(appname:="PT_Trans", Section:="phoneNumbers", _
Key:=defaultSite, Default:="")
On Error GoTo 0

' defaultPhone = ""

'fill out phone info at bottom of page
' ActiveDocument.lblQuestions = "If you have any questions or concerns please call " & defaultPhone

Selection.GoTo What:=wdGoToBookmark, Name:="beginDoc"

mainForm.Show

'get connection to db

Dim conn As New ADODB.Connection
Dim connectString As String
conn.Provider = "sqloledb"
connectString = "Server=bhs_hospice;Database=rehabTemplate;user id=USER;password=PWD;Trusted_Connection=yes"
conn.Open connectString

Dim Rs As New ADODB.Recordset
Dim sql As String

sql = "SELECT DISTINCT lineItems.groupID "
sql = sql & "FROM lineItems, templateGroups "
sql = sql & "WHERE lineItems.groupID = templateGroups.groupID "
'sql = sql & "AND templateGroups.templateName = 'ortho'" '& "'" & templateName & "' "


sql = sql & "AND templateGroups.templateName = '" & templateNameSetting.templateName & "' "
sql = sql & " ORDER BY lineItems.groupID"

Rs.Open sql, conn


'THE FORMS IN THIS WHILE LOOP GENERATE TEXT USING SELECTION.TYPETEXT STATEMENTS
While Not Rs.EOF

'open a form for each group
globalGroupID = Rs.Fields("groupID")
rehabInitial.Show
Rs.MoveNext

Wend

Selection.TypeParagraph
turnBulletsOff

Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="The patient's goals have been discussed with the patient and/or significant other."
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="_________________________________________"
Selection.TypeParagraph
Selection.TypeParagraph

Selection.MoveDown

'Open the Medicare form. (NOTE: This form contains a yes and a no button. If yes is clicked, a selection.typeText statement is run. If no is clicked, the form unloads.)

medicareForm.Show

'fill out phone info at bottom of page


Set ctrlLabel = ActiveDocument.InlineShapes.AddOLEControl(ClassType:="Forms.Label.1", Range:=Selection.Range)


This last line is the problem. If I omit the line all label events work fine. If I include the line the code in label.click never executes.




 
The only way to add event handler to a run-time created activex control is a class module and WithEvents declaration. The steps:
I. design mode:
1. add class module,
2. add WithEvents declaration (As MSForms.Label, Public),
3. create event procedure in class module for this variable,
II. run-time mode:
4. add control (label) to the document/form,
5. create an instance of the class (1),
6. assign the control (4) to the WithEvents declared variable (2) in the class instance (5).

combo
 
Thanks for the response combo. That will actually help me with another issue I have been thinking about.

If I understand correctly it does not apply in this case though. I have three different labels:
- Two are created in design mode at which time I add the form.show code.
- One is created at run-time. I have not coded any events for this control.

If I create only the design-time controls everything works fine. Once I add the run-time control, the events associated with the DESIGN-TIME controls no longer work.

 
Sorry, I misunderstood your problem. Seems that adding activex control (temporarily) blocks referencing controls to template event procedures. Hope that someone will find the solution.

combo
 
No problem. I appreciate your input as well as fumei's.

The funny thing is when I create a simple test document to replicate the problem everything works fine. So there is something else going on...
 
That is what I am trying to find out. WHAT is that something else? Because I can not duplicate it in my test document either.

I suspect it has something to do with where the Selection is. Where does:

Selection.MoveDown

go?

faq219-2884

Gerry
My paintings and sculpture
 
That line moves down to the next line in the document which is a blank line.

The exact position of this line within the document can change based on what text is generated in the while loop.

The line after Selection.MoveDown is medicareForm.Show. This form inserts text on the document at the Selection location and this text always works fine.

Does this help to answer your question?
 
I also experimented with moving around the line of code which caused this problem. From the very first line of code to the very last, it seems no matter where I place it the same problem happens.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top