W
Wooly
Hi All, I am new to VBA programming with word and am looking for help with an
issue that has me stumped.
I am trying to create a word form that, upon opening, will open a text file,
get a 6 digit string (the only content of the text file), put it in a text
form field (Receipt_Num) and then execute a macro that will display a
dropdown calendar and put the selected date in another text form field
(Letter_Date).
The Letter_Date "Run Macro on Entry" box has "OpenCalendar" in it.
There are more fields and macros in the form but they don't have any bearing
on the issue.
Both activities work independantly, and calendar activity works from within
the document, however if I put the code to read the number from the file and
then write the contents to the text form field in AutoOpen(), problems arise.
When I open the document everything works OK until the Line of asteriskes in
the AutoOpen() code at this point the programme jumps to Sub OpenCalendar()
and starts into the Private Sub UserForm_Initialize() code, when it gets to
the line:
CurrentEntry = ActiveDocument.FormFields(ActiveField).Result
it writes (correct) data to the Receipt_Num field and then continues on into
the Private Sub Calendar1_Click() and then produces an error message on the
code after the
asterisks, the error is:
Microsoft Visual Basic
Run-time error -2147467259 (80004005)
Method 'Result' of object 'FormField' failed
If I hit the END button on the error message I can then use the form as
usual, i.e. go to the Letter_Date field select the date from the calendar and
it will go into the
fiels as expected.
Any insight would be greatly appreciated.
Wooly
EDITED: It appears that the programme will "go off course" into what ever
macro is identified in the The Letter_Date "Run Macro on Entry" box
________________________________________________________________
Sub AutoOpen()
Dim Store_Receipt_Number_File As String
Dim Current_Receipt_Number As Long
Dim Last_Receipt_Number As String
Store_Receipt_Number_File =
"J:\Foundation\Tax_Receipts\Admin\Last_Receipt_Number.txt" 'File that stores
the last number
FNum = FreeFile
Open Store_Receipt_Number_File For Input As FNum
Input #FNum, Last_Receipt_Number
Close FNum
Current_Receipt_Number = CLng(Last_Receipt_Number) + 1 'Increment last
record number
**********THIS IS WHERE IT GOES WRONG***************
ActiveDocument.FormFields("Receipt_Num").Result = Current_Receipt_Number
'Put it in document
End Sub
________________________________________________________________
Sub
many more macros
End Sub
Sub AutoClose()
bla
bla
bla
End Sub
________________________________________________________________
Sub OpenCalendar()
frmCalendar.Show
End Sub
________________________________________________________________
________________________________________________________________
Private Sub UserForm_Initialize()
Dim ActiveField As String
Dim CurrentEntry As String
ActiveField = Selection.Bookmarks(1).Name
CurrentEntry = ActiveDocument.FormFields(ActiveField).Result
If IsDate(CurrentEntry) Then
Calendar1.Value = DateValue(CurrentEntry)
Else
Calendar1.Value = Date
End If
End Sub
________________________________________________________________
Private Sub Calendar1_Click()
Dim ActiveField As String
ActiveField = Selection.Bookmarks(1).Name
*****************Error occurs on following line*****************
ActiveDocument.FormFields(ActiveField).Result = Format(Calendar1.Value, "dd
mmmm yyyy")
Unload Me
End Sub
________________________________________________________________
Private Sub cmdClose_Click()
Unload Me
End Sub
issue that has me stumped.
I am trying to create a word form that, upon opening, will open a text file,
get a 6 digit string (the only content of the text file), put it in a text
form field (Receipt_Num) and then execute a macro that will display a
dropdown calendar and put the selected date in another text form field
(Letter_Date).
The Letter_Date "Run Macro on Entry" box has "OpenCalendar" in it.
There are more fields and macros in the form but they don't have any bearing
on the issue.
Both activities work independantly, and calendar activity works from within
the document, however if I put the code to read the number from the file and
then write the contents to the text form field in AutoOpen(), problems arise.
When I open the document everything works OK until the Line of asteriskes in
the AutoOpen() code at this point the programme jumps to Sub OpenCalendar()
and starts into the Private Sub UserForm_Initialize() code, when it gets to
the line:
CurrentEntry = ActiveDocument.FormFields(ActiveField).Result
it writes (correct) data to the Receipt_Num field and then continues on into
the Private Sub Calendar1_Click() and then produces an error message on the
code after the
asterisks, the error is:
Microsoft Visual Basic
Run-time error -2147467259 (80004005)
Method 'Result' of object 'FormField' failed
If I hit the END button on the error message I can then use the form as
usual, i.e. go to the Letter_Date field select the date from the calendar and
it will go into the
fiels as expected.
Any insight would be greatly appreciated.
Wooly
EDITED: It appears that the programme will "go off course" into what ever
macro is identified in the The Letter_Date "Run Macro on Entry" box
________________________________________________________________
Sub AutoOpen()
Dim Store_Receipt_Number_File As String
Dim Current_Receipt_Number As Long
Dim Last_Receipt_Number As String
Store_Receipt_Number_File =
"J:\Foundation\Tax_Receipts\Admin\Last_Receipt_Number.txt" 'File that stores
the last number
FNum = FreeFile
Open Store_Receipt_Number_File For Input As FNum
Input #FNum, Last_Receipt_Number
Close FNum
Current_Receipt_Number = CLng(Last_Receipt_Number) + 1 'Increment last
record number
**********THIS IS WHERE IT GOES WRONG***************
ActiveDocument.FormFields("Receipt_Num").Result = Current_Receipt_Number
'Put it in document
End Sub
________________________________________________________________
Sub
many more macros
End Sub
Sub AutoClose()
bla
bla
bla
End Sub
________________________________________________________________
Sub OpenCalendar()
frmCalendar.Show
End Sub
________________________________________________________________
________________________________________________________________
Private Sub UserForm_Initialize()
Dim ActiveField As String
Dim CurrentEntry As String
ActiveField = Selection.Bookmarks(1).Name
CurrentEntry = ActiveDocument.FormFields(ActiveField).Result
If IsDate(CurrentEntry) Then
Calendar1.Value = DateValue(CurrentEntry)
Else
Calendar1.Value = Date
End If
End Sub
________________________________________________________________
Private Sub Calendar1_Click()
Dim ActiveField As String
ActiveField = Selection.Bookmarks(1).Name
*****************Error occurs on following line*****************
ActiveDocument.FormFields(ActiveField).Result = Format(Calendar1.Value, "dd
mmmm yyyy")
Unload Me
End Sub
________________________________________________________________
Private Sub cmdClose_Click()
Unload Me
End Sub