PDA

View Full Version : Userform help



Birch81
04-28-2010, 02:16 PM
Hello,

I pretty new to Word VBA so I need some help :)

I have made a userform that has to transfer some numbers and text to some bookmarks in word.

I have some trouble transfering the right values to my bookmarks.I have the following code when the OK button is pressed.
If txtStandardInvestering.Value differs from 0 it has to write the value of txtStandardInvestering.Text to my bookmark.


Private Sub cbOK_Click()
Dim strStdInv As String
If cboNordElSyd = "Nord" Then
If txtStandardInvestering.Value <> 0 Then
strStdInv = txtStandardInvestering.Text
End If
End If
Application.ScreenUpdating = False
With ActiveDocument
.Bookmarks("StandardInvestering").Range.Text = strStdInv
End With
Application.ScreenUpdating = True

Unload Me
End Sub

No values are transfered to my bookmark "StandardInvestering" when I use that code.

If I use this code insted it works fine
.Bookmarks("StandardInvestering").Range.Text = txtStandardInvestering.Text

but thatīs not the way it has to work.

Can anyone help me?

Birch81

Edit: VBA Tags added to code

lucas
04-28-2010, 08:30 PM
works for me if you select Nord from the combo box.

whatever text is in the textbox goes to the bookmark.

Birch, when posting code, select it and hit the green vba button to format it for the forum.

Birch81
05-12-2010, 11:30 AM
works for me if you select Nord from the combo box.

whatever text is in the textbox goes to the bookmark.

Birch, when posting code, select it and hit the green vba button to format it for the forum.

Hey,

Yeah now it works for me to ? Wierd :)

But thanks for the reply.

Iīll use the VBA button from now on :)

Birch81
05-12-2010, 11:35 AM
I have another question.

I have the following code where som of the text has to be underlined. How do I do this?

If txtStandardInvestering.Text <> "" Then
.Bookmarks("StandardInvestering").Range.Text = "Standard investeringsbidrag" & vbCrLf _
& vbTab & "(" & txtStandardInvestering.Text & " stk. x " & Format(txtPrisStandardInvesteringsbidragPris.Text, "##,##0.00") & " kr.)" & vbTab

.Bookmarks("StandardInvesteringPris").Range.Text = "kr." & vbTab & _
Format((txtStandardInvestering.Text * txtPrisStandardInvesteringsbidragPris.Text), "##,##0.00") & vbCrLf & vbTab


End If

i want to underline .Bookmarks("StandardInvesteringPris").Range.Text = "kr." & vbTab & _
Format((txtStandardInvestering.Text * txtPrisStandardInvesteringsbidragPris.Text), "##,##0.00") & vbCrLf & vbTab


/Birch81

fumei
05-12-2010, 11:54 AM
Have the bookmark already underlined. Better yet, use styles.

Birch81
05-12-2010, 12:03 PM
Have the bookmark already underlined. Better yet, use styles.

Hey Gerry,

Can you give med a hint :)

fumei
05-12-2010, 12:16 PM
1. Have the bookmark StandardInvesteringPris already underlined.

Although, btw, using range.text like that will delete the bookmark. So make sure the character AFTER the bookmark (while it exists) is underlined.

As an aside, there is an easy way to put text INTO a bookmark, and keeping the bookmark. This is handy if you ever want to use the bookmark twice, or get information OUT of it.

2. Styles. Styles are explicitly defined formats. That is it.
ActiveDocument.Bookmarks("Yadda").Range.Style = "Whatever"
would apply the format of the Whatever style to the range of the bookmark Yadda.

ALL paragraphs, ALL characters in a Word document use styles. ALL.

Birch81
05-13-2010, 11:14 PM
Hey Gerry,

I canīt have the bookmark underlined from start because itīs not always the same line that has to be underlined. It depends on how many textboxes that has a value.

Birch81

Tinbendr
05-14-2010, 04:59 AM
I canīt have the bookmark underlined from start because itīs not always the same line that has to be underlined.You could if all the bookmarks are empty, because an empty bookmark won't be underlined.

or try this.

Sub UsingBookmarks()
Dim MyText$

With ActiveDocument
MyText$ = "kr." & vbTab & _
Format((txtStandardInvestering.Text * txtPrisStandardInvesteringsbidragPris.Text), _
"##,##0.00") & vbCrLf & vbTab
Call UpdateBookmark("StandardInvesteringPris", MyText$)

'Change the format of the bookmark here. Bold, underline, etc.
'You can move this to the UpdateBookmark Sub if every bookmark is underlined.
.Bookmarks("StandardInvesteringPris").Range.Font.Underline = wdUnderlineThick

End With

End Sub

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
As Gerry mentioned earlier, when you write to a bookmark, you delete it. This method preserves the bookmark, thus allowing you to add embellishments.

fumei
05-17-2010, 10:10 AM
Or change the bookmark filling procedure to include a format attribute.
Sub FillBM(strBM As String, strText As String, bolUnderline As Boolean)
Dim r As Range
Set r = ActiveDocument.Bookmarks(strBM).Range
r.Text = strText
ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
If bolUnderline Then
ActiveBookmarks(strBM).Range.Font.Underline = wdUnderlineThick
End If
End Sub


Now you can fill the bookmark AND underline it by passing a True parameter. Using Tinbendr's example...
Call FillBM("StandardInvesteringPris", MyText$, True)
The bookmark StandardInvesteringPris is filled with MyText$ and is underlined.

fumei
05-17-2010, 10:18 AM
Hint to newbies...

In the above post, I have:
If bolUnderline Then
ActiveBookmarks(strBM).Range.Font.Underline = wdUnderlineThick
End If
Technically, this is correct, because the default "meaning" - because it is an IF statement - of bolUnderline is "IF bolUnderline = True Then".

Therefore you do not have to use the = True.

If bolUnderline Then

is EXACTLY (technically) the same as

If bolUnderline = True Then

However, again to the newbies, I would strongly recommend you use the fully qualified syntax - use the = True.

Also, because the boolean parameter is not Optional, you MUST include something.
[vba]
Call FillBM("StandardInvesteringPris", MyText$, True) = passes TRUE
Call FillBM("StandardInvesteringPris", MyText$, False) = passes FALSE
Call FillBM("StandardInvesteringPris", MyText$, ,) = passes FALSE

In other words, if you do NOT want the underline you MUST add the comma - bolUnderline is a required parameter.

Call FillBM("StandardInvesteringPris", MyText$) - nothing passed for bolUnderline - will fail.