PDA

View Full Version : Solved: Copy variables from UserForm to open Document



mastersnowbo
08-25-2005, 12:28 AM
hi,

i have a userform that opens when a document is opened.

On the UserForm is a combo box, a label and a text box - i need those values to be copied to 3 labels within the open document

Any help would me greatly appreciated

Steiner
08-25-2005, 04:47 AM
Hi and welcome here.
How are the "labels" within the document defined? Let's assume you have 3 bookmarks (bm_combo, bm_label, bm_text) then the code could be:
1. This is a small method I generally use for inserting text:

Public Sub WriteBookmark(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
End If
End Sub


2. In this case you could use it like that:
Private Sub CommandButton1_Click()
WriteBookmark "bm_combo", ComboBox1.Text
WriteBookmark "bm_label", Label1.Caption
WriteBookmark "bm_text", TextBox1.Text
End Sub


Daniel

MOS MASTER
08-25-2005, 02:50 PM
Hi and welcome to VBAX! :hi:

Indeed labels in the document is a bit criptic....

There are many ways of doing this job and Daniel's way is a nice way to get a fully (Ranged) bookmark in the document.

But like he said we need to no exacly where the data should be written to..e.g: Form field; field; bookmark; activeX control; Variable; etc....

HTH, :whistle:

mastersnowbo
08-25-2005, 07:45 PM
hi again, thanks for the quick replies - my first msg was a bit vague. i think i solved it. they enter/select the oppropriate details in a user form, and then after a few 'if's' if they've done it right it loads this

Private Sub Document_Refresh()
AREA = UserForm1.cmboArea.Text
PROJ = UserForm1.txtProj.Value
DAIT = UserForm1.lbldate.Caption

lblarea.Caption = AREA
lblname.Caption = PROJ
lbldate2.Caption = DAIT
End Sub

i didn't know i could do ''UserForm1.cmboArea.Text" type commands.

as you can tell, i don't know much!

thanks again!

MOS MASTER
08-26-2005, 12:23 PM
Glad to see you have it working.

I still don't know for sure what type of label you are refering to but gudging by the code I'd think they are ActiveX embeded in the document body.

Later..:moosegrin