Amy was telling us:
Amy nous racontait que :
Thanks for looking into this. In a separate procedure under Modules
I have: frmCurrentDept.Show.
And this is the code for my user form:
Option Explicit
Dim sourcedoc As Document
Dim intOne As Integer
Dim intTwo As Integer
Dim rngDept As Range
Dim rngTitle As Range
Private Sub cboDept_Change()
'Remove any existing items in cboTitle
If cboTitle.ListCount >= 1 Then
For intTwo = cboTitle.ListCount To 1 Step -1
cboTitle.RemoveItem (intTwo)
Next intTwo
This can all be replaced with:
cboTitle.Clear
Any particular reason why you have this complicated process to clear the
combobox when one simple line can do it?
End If
Application.ScreenUpdating = False
'Open the file containing the form data details
Set sourcedoc = Documents.Open(FileName:="G:\HRData102605.doc")
'"Q\Departments\Human Resources\Readonly\HRData.doc")
intTwo = cboDept.ListIndex + 2
For intOne = 1 To sourcedoc.Tables(1).Cell(intTwo,
2).Range.Paragraphs.Count Set rngTitle =
sourcedoc.Tables(1).Cell(intTwo, 2).Range.Paragraphs(intOne).Range
rngTitle.End = rngTitle.End - 1
cboTitle.AddItem rngTitle
Next intOne
End Sub
Private Sub UserForm_initialize()
On Error Resume Next
Remove this line and debug the code line by line to see which line causes
the problem.
You could add code to set a variable to point to the original Document.
At the top (with the other Dim statements) add
Dim OriginDoc As Document
Then in the Initialize Sub:
Set OriginDoc = ActiveDocument
or
Set OriginDoc = Documents("Name of Doc")
if the document always has the same name.
Application.ScreenUpdating = False
'open the file containing the form data
Set sourcedoc = Documents.Open(FileName:="G:\HRData102605.doc")
'"Q\Departments\Human Resources\Readonly\HRData.doc")
For intOne = 2 To sourcedoc.Tables(1).Rows.Count
Set rngDept = sourcedoc.Tables(1).Cell(intOne, 1).Range
rngDept.End = rngDept.End - 1
cboDept.AddItem rngDept
Next intOne
'close the file containing the department details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Private Sub cmdClose_Click()
Here you could use OriginDoc to make sure you are inserting the values in
the proper document:
With OriginDoc
.FormFields("bkDept1").Result = cboDept.Value
.FormFields("bkTitle1").Result = cboTitle.Value
End With
Note that this will remove the bookmarks. If you do not need the bookmarks
anymore, then it does not matter, if you do (If the userform is called a
second time for example) then let us know we will show you how to save the
bookmarks.
ActiveDocument.FormFields("bkDept1").Result = cboDept.Value
ActiveDocument.FormFields("bkTitle1").Result = cboTitle.Value
Unload Me
Note that once this line executes, the form is wiped out from memory so that
you cannot refer to it anymore.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org