Trouble with VBA Code in userform.... command_click event

Hi everyone,

I am getting frustrated with this Userform, I ahve read everywhere trying to get this thin to work, and finally decided to consult you guys,

I have a userform, I cant seem to grasp the concept of where to put the code. I have a cmdOK_click() event that I am trying to get to work. I want to click ok, and populate book marks. Previously, I was using the .range method to populate the bookmarks but this was destroying to bookmarks. I got som suggestions from this site and from researching books and internet and am trying to get the following code to run with a click of the ok button:
Public Sub wb(ByVal BName As String, ByVal inhalt As String)

     'Sub to enter text in Bookmark location
Private Sub wb(ByVal BName As String, ByVal inhalt As String)
       If ActiveDocument.Bookmarks.Exists(BName) Then
        Dim r As Range
        Set r = ActiveDocument.Bookmarks(BName).Range
        r.Text = inhalt
        ActiveDocument.Bookmarks.Add BName, r
    Else
        Debug.Print "Bookmark not found: " & BName
    End If
End Sub
    If ActiveDocument.Bookmarks.Exists(BName) Then
        Dim r As Range
        Set r = ActiveDocument.Bookmarks(BName).Range
        r.Text = inhalt
        ActiveDocument.Bookmarks.Add BName, r
    Else
        MsgBox "Bookmark not found: " & BName
    End If
   
End Sub

 Steiner http://www.vbaexpress.com/kb/getarticle.php?kb_id=126

I cant put it my click procedure, when i put it in a module nothing hapens, I know it works in VBA because I have an example that a EE member was kind enough to give me, and it works fine on his form, but wont on mine!!!!

I have moved it all around and nothing. I have a set of variables:

wb "bookmarhname", "My Text To Insert inti Bookmark"
WB "bookmarkname", combobox1.value

The sample that I have has the wb XXX, XXX in a commond click procedure. When I put mine in I get an "expected sub, function or property error" ????
Any Ideas??
COdy
codydoyleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

R_RajeshCommented:
put this in your cmdOK_click() event. the code has and array that consists of the bookmark name and the corresponding text. once you click the command button it goes through each array element and populates the existing bookmarks in the document (here bmk_1 is the bookmark name and text of bookmark1 is its text)


Private Sub cmdOK_Click()
Dim bmarr(1 To 5, 1 To 2) As String
bmarr(1, 1) = "bmk_1": bmarr(1, 2) = "Text of bookmark1"
bmarr(2, 1) = "bmk_2": bmarr(2, 2) = "Text of bookmark2"
bmarr(3, 1) = "bmk_3": bmarr(3, 2) = "Text of bookmark3"
bmarr(4, 1) = "bmk_4": bmarr(4, 2) = "Text of bookmark4"
bmarr(5, 1) = "bmk_5": bmarr(5, 2) = "Text of bookmark5"
For i = 1 To 5
If ActiveDocument.Bookmarks.Exists(bmarr(i, 1)) Then
Dim r As Range
Set r = ActiveDocument.Bookmarks(bmarr(i, 1)).Range
r.Text = bmarr(i, 2)
ActiveDocument.Bookmarks.Add bmarr(i, 1), r
End If
Next i
End Sub


Rajesh
0
GrahamSkanRetiredCommented:
Hi Cody,
If you get an error in code, it is helpful to post the exact code, the error message and the highlighted error line (or part).

This should work.
Private Sub cmdOK_Click
      wb "bookmarhname", "My Text To Insert inti Bookmark"
end sub

I'm not sure why this code is declared Private. I suggest that it be put in a module and set Public as under.

Public Sub wb(ByVal BName As String, ByVal inhalt As String)
       Dim r As Range
       If ActiveDocument.Bookmarks.Exists(BName) Then
             Set r = ActiveDocument.Bookmarks(BName).Range
             r.Text = inhalt
             ActiveDocument.Bookmarks.Add BName, r
       Else
             Debug.Print "Bookmark not found: " & BName
    End If
End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteinerCommented:
Graham is right, make WB public (as it is in the KB-entry on VBAExpress) or just put the code into the same userform where you plan to call it from, then it should work.
0
codydoyleAuthor Commented:
Thanks again everyone!!!!,
Worked perfect!!!! I wish I had of written the exact error code I was getting so I could understand where my logic is flawed.... Anyway, when I would put the wb="....","..." , in the cmdok_click procedure it would highlight th firse wb and say somthing like "expected end sub" or "sub"??? Any way thanks
 again, oh and Rajash, thanks for the code, could not get it to work but  did not spend alot of time on it....... Thanks for the response, and Grahm, Thanks again!!!! Your a life saver!!

Cody.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.