[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-17
4
Medium Priority
?
416 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:codydoyle
4 Comments
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 12611356
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 12612969
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
 
LVL 11

Expert Comment

by:Steiner
ID: 12613021
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
 

Author Comment

by:codydoyle
ID: 12627793
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question