PDA

View Full Version : {word for mac} Runtime error 5148 The number must be between {b} and {b}



bmichelle510
10-19-2012, 09:35 AM
Hello,
I am working in Word for Mac. There is a user form that collects the data. Then the data needs to be inserted into the document. Some of the data has to be verified first and strings are assigned to variables. When I debug it takes me to the first line in the "with" statement. When I run the same program on a PC I get a different error. It says the bookmark can not be deleted and takes me to the same line of code. I would really appreciate some help.
Thank You

Option Explicit
Dim strCourtName As String
Dim strCourtDivision As String
Dim strCourtStreetAddress As String
Dim strCourtCityAddress As String
Dim strProsecutorStreetAddress As String
Dim strProsecutorCityAddress As String

Private Sub CmdbtCancel_Click()
Me.Hide
End Sub

Private Sub CmdbtnSubmit_Click()

If TxtbxCauseCourt.Value = "45D07" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION I"
strCourtCityAddress = "CROWN POINT, INDIANA"
End If

If TxtbxCauseCourt.Value = "45D08" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION II"
strCourtCityAddress = "CROWN POINT, INDIANA"
End If

If TxtbxCauseCourt.Value = "45D09" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION III"

End If

If TxtbxCauseCourt.Value = "45D12" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION IV"
strCourtCityAddress = "HAMMOND, INDIANA"
End If

If TxtbxCauseCourt.Value = "45G01" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "ROOM NUMBER ONE"
strCourtCityAddress = "CROWN POINT, INDIANA"
End If

If TxtbxCauseCourt.Value = "45G02" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "ROOM NUMBER TWO"
strCourtCityAddress = "CROWN POINT, INDIANA"
End If

If TxtbxCauseCourt.Value = "45G03" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "ROOM NUMBER THREE"
strCourtCityAddress = "CROWN POINT, INDIANA"
End If

If TxtbxCauseCourt.Value = "45G04" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "ROOM NUMBER FOUR"
strCourtCityAddress = "CROWN POINT, INDIANA"
End If

If TxtbxCauseCourt.Value = "45G09" Then
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION III"
strCourtCityAddress = "CROWN POINT, INDIANA"
End If

If TxtbxCauseCourt.Value = "45H01" Then
strCourtName = "CROWN POINT CITY COURT"
strCourtCityAddress = "CROWN POINT, INDIANA"
End If

If TxtbxCauseCourt.Value = "45H02" Then
strCourtName = "EAST CHICAGO CITY COURT"
strCourtCityAddress = "EAST CHICAGO, INDIANA"
End If

If TxtbxCauseCourt.Value = "45H04" Then
strCourtName = "HAMMOND CITY COURT"
strCourtCityAddress = "HAMMOND, INDIANA"
End If

If TxtbxCauseCourt.Value = "45H05" Then
strCourtName = "HOBART CITY COURT"
strCourtCityAddress = "HOBART, INDIANA"
End If

If TxtbxCauseCourt.Value = "45H06" Then
strCourtName = "LAKE STATION CITY COURT"
strCourtCityAddress = "LAKE STATION, INDIANA"
End If

If TxtbxCauseCourt.Value = "45H07" Then
strCourtName = "WHITING CITY COURT"
strCourtCityAddress = "WHITING, INDIANA"
End If

If TxtbxCauseCourt.Value = "45I02" Then
strCourtName = "SCHERERVILLE TOWN COURT"
strCourtCityAddress = "SCHERERVILLE, INDIANA"
End If

If TxtbxCauseCourt.Value = "45I03" Then
strCourtName = "LOWELL TOWN COURT"
strCourtCityAddress = "LOWELL, INDIANA"
End If

With ActiveDocument
.Bookmarks("bmCourtName").Range.Text = strCourtName
.Bookmarks("bmCourtDivision").Range.Text = strCourtDivision
.Bookmarks("bmCourtCityAddress").Range.Text = strCourtCityAddress
.Bookmarks("bmFirstName").Range.Text = TxtbxFirstName.Value
.Bookmarks("bmLastName").Range.Text = TxtbxLastName.Value
.Bookmarks("bmCauseCourt").Range.Text = TxtbxCauseCourt.Value
.Bookmarks("bmCauseYrMth").Range.Text = TxtbxCauseYrMth.Value
.Bookmarks("bmCauseType").Range.Text = TxtbxCauseType.Value
.Bookmarks("bmCauseNo").Range.Text = TxtbxCauseNo.Value
End With

If cbInitiating.Value = True Then
ActiveDocument.Bookmarks("bmInitiating").Range.Text = "__X__"
End If

If cbResponding.Value = True Then
ActiveDocument.Bookmarks("bmResponding").Range.Text = "__X__"
End If

If cbIntervening.Value = True Then
ActiveDocument.Bookmarks("bmIntervening").Range.Text = "__X__"
End If

If cbOtherPartiesYes.Value = True Then
ActiveDocument.Bookmarks("bmOtherPartiesYes").Range.Text = "__X__"
End If

If cbOtherPartiesNo.Value = True Then
ActiveDocument.Bookmarks("bmOtherPartiesYes").Range.Text = "__X__"
End If

If cbSupportIssuesYes.Value = True Then
ActiveDocument.Bookmarks("bmSupportIssuesYes").Range.Text = "__X__"
End If

If cbSupportIssuesNo.Value = True Then
ActiveDocument.Bookmarks("bmSupportIssuesNo").Range.Text = "__X__"
End If

If cbRelatedCasesYes.Value = True Then
ActiveDocument.Bookmarks("bmRelatedCasesYes").Range.Text = "__X__"
End If

If cbRelatedCasesNo.Value = True Then
ActiveDocument.Bookmarks("bmRelatedCasesNo").Range.Text = "__X__"
End If

If cbOtherPartiesServedYes.Value = True Then
ActiveDocument.Bookmarks("bmPartiesServedYes").Range.Text = "__X__"
End If

If cbOtherPartiesServedNo.Value = True Then
ActiveDocument.Bookmarks("bmPartiesServedNo").Range.Text = "__X__"
End If

Unload Me
End Sub

Frosty
10-19-2012, 10:19 AM
Couple of quick comments/questions
1. Why are you only Hiding the form when you hit the cancel button, but unloading it when you hit the Submit button? It will stay in memory (and retain old values) if you hit the cancel button... that may not be desirable, especially since some of your logic sets a City Address and some do not... (Lake Superior Court, Division III, for example)

2. Great time to explore a Select Case logic structure, rather than a series of If ... End If all based on the contents of a single value (TxtbxCauseCourt.Value) -- I've done this for you.

3. Using With blocks is a great way to simplify coding... however, do you know for sure that you have an ActiveDocument when this form is run? Is the Form Modal or Modeless (i.e., can you do other stuff on the active document or switch to another document when the form is displayed, or is all interaction with Word disabled while the form is displayed?)

4. Is the document you're trying to affect (containing all the bookmarks) some kind of protected document? If you open the document (and the bookmark "bmCourtName" exists), can you run the following line of code in the immediate window?
ActiveDocument.Bookmarks("bmCourtName").Range.Text = "Test Court Name info"

5. Don't use Dim statements outside of subroutines or functions. It is effectively the same as using Private, but Private is more clear. My changing of all your "Dim" declarations to "Private" is functionally the same, but a much better coding practice.


Option Explicit
Private strCourtName As String
Private strCourtDivision As String
Private strCourtStreetAddress As String
Private strCourtCityAddress As String
Private strProsecutorStreetAddress As String
Private strProsecutorCityAddress As String

Private Sub CmdbtCancel_Click()
Me.Hide
End Sub

Private Sub CmdbtnSubmit_Click()

'Gather Info...
Select Case TxtbxCauseCourt.Value
Case "45D07"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION I"
strCourtCityAddress = "CROWN POINT, INDIANA"

Case "45D08"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION II"
strCourtCityAddress = "CROWN POINT, INDIANA"

Case "45D09"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION III"

Case "45D12"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION IV"
strCourtCityAddress = "HAMMOND, INDIANA"

Case "45G01"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "ROOM NUMBER ONE"
strCourtCityAddress = "CROWN POINT, INDIANA"

Case "45G02"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "ROOM NUMBER TWO"
strCourtCityAddress = "CROWN POINT, INDIANA"

Case "45G03"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "ROOM NUMBER THREE"
strCourtCityAddress = "CROWN POINT, INDIANA"

Case "45G04"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "ROOM NUMBER FOUR"
strCourtCityAddress = "CROWN POINT, INDIANA"

Case "45G09"
strCourtName = "LAKE SUPERIOR COURT"
strCourtDivision = "DIVISION III"
strCourtCityAddress = "CROWN POINT, INDIANA"

Case "45H01"
strCourtName = "CROWN POINT CITY COURT"
strCourtCityAddress = "CROWN POINT, INDIANA"

Case "45H02"
strCourtName = "EAST CHICAGO CITY COURT"
strCourtCityAddress = "EAST CHICAGO, INDIANA"

Case "45H04"
strCourtName = "HAMMOND CITY COURT"
strCourtCityAddress = "HAMMOND, INDIANA"

Case "45H05"
strCourtName = "HOBART CITY COURT"
strCourtCityAddress = "HOBART, INDIANA"

Case "45H06"
strCourtName = "LAKE STATION CITY COURT"
strCourtCityAddress = "LAKE STATION, INDIANA"

Case "45H07"
strCourtName = "WHITING CITY COURT"
strCourtCityAddress = "WHITING, INDIANA"

Case "45I02"
strCourtName = "SCHERERVILLE TOWN COURT"
strCourtCityAddress = "SCHERERVILLE, INDIANA"

Case "45I03"
strCourtName = "LOWELL TOWN COURT"
strCourtCityAddress = "LOWELL, INDIANA"

End Select

'put that info into the active document
With ActiveDocument
.Bookmarks("bmCourtName").Range.Text = strCourtName
.Bookmarks("bmCourtDivision").Range.Text = strCourtDivision
.Bookmarks("bmCourtCityAddress").Range.Text = strCourtCityAddress
.Bookmarks("bmFirstName").Range.Text = TxtbxFirstName.Value
.Bookmarks("bmLastName").Range.Text = TxtbxLastName.Value
.Bookmarks("bmCauseCourt").Range.Text = TxtbxCauseCourt.Value
.Bookmarks("bmCauseYrMth").Range.Text = TxtbxCauseYrMth.Value
.Bookmarks("bmCauseType").Range.Text = TxtbxCauseType.Value
.Bookmarks("bmCauseNo").Range.Text = TxtbxCauseNo.Value

If cbInitiating.Value = True Then
.Bookmarks("bmInitiating").Range.Text = "__X__"
End If

If cbResponding.Value = True Then
.Bookmarks("bmResponding").Range.Text = "__X__"
End If

If cbIntervening.Value = True Then
.Bookmarks("bmIntervening").Range.Text = "__X__"
End If

If cbOtherPartiesYes.Value = True Then
.Bookmarks("bmOtherPartiesYes").Range.Text = "__X__"
End If

If cbOtherPartiesNo.Value = True Then
.Bookmarks("bmOtherPartiesYes").Range.Text = "__X__"
End If

If cbSupportIssuesYes.Value = True Then
.Bookmarks("bmSupportIssuesYes").Range.Text = "__X__"
End If

If cbSupportIssuesNo.Value = True Then
.Bookmarks("bmSupportIssuesNo").Range.Text = "__X__"
End If

If cbRelatedCasesYes.Value = True Then
.Bookmarks("bmRelatedCasesYes").Range.Text = "__X__"
End If

If cbRelatedCasesNo.Value = True Then
.Bookmarks("bmRelatedCasesNo").Range.Text = "__X__"
End If

If cbOtherPartiesServedYes.Value = True Then
.Bookmarks("bmPartiesServedYes").Range.Text = "__X__"
End If

If cbOtherPartiesServedNo.Value = True Then
.Bookmarks("bmPartiesServedNo").Range.Text = "__X__"
End If
End With

Unload Me
End Sub

bmichelle510
10-19-2012, 10:41 AM
Hi,
And thank you for replying. I do apologize about the cross posting. I had this problem the other day, but some how resolved it. After adding more code the problem is back.
Answers to questions:
1. Not sure, but I will unload in both now
2. Very good point, I noticed that just after I posted and did not want to make any changes until I saw if I would get a reply. I once made changes to a program I was working on and had posted for help and learned that is not the way to do it.
3. I am pretty sure it is modal. I am new to this so I am not positive.
4. I did not set it to be protected. I tried putting the line of code you suggested in the immediate window but nothing ran differently.
5. I will change my Dim statements.
I have to run an errand now so I will not be able to reply for awhile should you continue to help me. I do appreciate the help and will check back as soon as I can. Thank You

bmichelle510
10-19-2012, 01:13 PM
Hi Frosty,
Yes, all interaction with Word is disabled while the form is displayed.
So, I need to test to see if the document is active?

macropod
10-19-2012, 03:34 PM
Cross-posted (days ago) at: http://www.tek-tips.com/viewthread.cfm?qid=1696040
AND YOU NEVER BOTHERED RESPONDING TO ANYTHING THERE!!!

bmichelle510
10-21-2012, 08:51 AM
I apologize. I checked all day and didn't get any help. I then worked on it that evening and the problem was gone but now it is back. I looked at the other forums before I came to the one but there were no replies

macropod
10-21-2012, 02:46 PM
As I said a mere 6 hours after you posted at Tek-Tips on 17 October:

The code you posted does not appear to be capable of generating RuntimeError 5148.
I also asked that you provide:

advice on what code line gets highlighted when the error occurs and what the two values are.

bmichelle510
10-21-2012, 06:27 PM
The line that gets highlighted is the first line of the with statement.
.bookmarks("bmCourtName").range.text= strCourtName
I only get the 5148 runtime error when it is run on a Mac
If it is run on a PC the error says the bookmark cannot be deleted

I am sorry for taking up your time on the other forum, that was not my intention
I did not receive any notifications that there were replies and when I checked there weren't any. I will learn from my mistakes

macropod
10-21-2012, 09:34 PM
Are you, perchance, trying to populate formfields with those bookmark names in a document protected for forms, or only ordinary bookmarks?

If you are updating formfields, do so via code like the following:
Dim Pwd As String
Pwd = "" 'Forms protection password
With ActiveDocument
.Unprotect Password:=Pwd
.Bookmarks("bmCourtName").Range.FormFields(1).Result.Text = strCourtName
.Bookmarks("bmCourtDivision").Range.FormFields(1).Result.Text = strCourtDivision
'etc
'...
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd
End With

fumei
10-21-2012, 11:15 PM
And just to point out that notification of other postings is not always automatic.

bmichelle510
10-22-2012, 07:53 AM
Macropod,
Yes, I am trying to populate form fields with the bookmarks, but the document is not protected.
Thank You

bmichelle510
10-22-2012, 08:06 AM
Fumei,
Thank you for the information.

bmichelle510
10-22-2012, 09:14 AM
Changing to
[VBA] .Bookmarks("bmCourtName").Range.Fields(1).Result.Text = strCourtName/VBA]
cleared up the problem
Thank both very much for your help

bmichelle510
10-22-2012, 11:33 AM
I just want to check before I make anyone mad.
If I have a different question about the same program I should start a new thread or use this same one?

Frosty
10-22-2012, 12:08 PM
Go ahead and start a new thread for the new problem. That way anyone who comes along can see the question and answer for this topic, as well as the question and answer for your new one.