Home |
Search |
Today's Posts |
|
#1
Posted to microsoft.public.word.newusers
|
|||
|
|||
Help with VBA / Macros Word 2003 Form please
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_Num ber.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 |
#2
Posted to microsoft.public.word.newusers
|
|||
|
|||
Help with VBA / Macros Word 2003 Form please
See the article "Creating sequentially numbered documents (such as
invoices)" at: http://www.word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm for an idea of how to get hold of the number. I would suggest that you consider using a userform rather than a document that is protected for forms. See the article "How to create a Userform" at: http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "Wooly" wrote in message ... 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_Num ber.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 |
#3
Posted to microsoft.public.word.newusers
|
|||
|
|||
Help with VBA / Macros Word 2003 Form please
Thanks for the response Doug, I have indeed moved to the Userform concept. I
guess more than anything now I am interested as to why the original programme went off the tracks, this type of "apparent" illogigcal behaviour bugs me :-) Wooly "Doug Robbins - Word MVP" wrote: See the article "Creating sequentially numbered documents (such as invoices)" at: http://www.word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm for an idea of how to get hold of the number. I would suggest that you consider using a userform rather than a document that is protected for forms. See the article "How to create a Userform" at: http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "Wooly" wrote in message ... 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_Num ber.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 |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Form Macros, Form Output to DB | Microsoft Word Help | |||
Automating form field entries in Word 2003 with macros | Microsoft Word Help | |||
Automate a form by saving macros in the form template...? | Microsoft Word Help | |||
Distributing Word Form with VBA Macros | Microsoft Word Help | |||
Macros in MS Word 2003 | Microsoft Word Help |