PDA

View Full Version : How do I use userform to create an entry right in the document in Word 2010?



zachkirsch
06-26-2013, 03:56 PM
I need some help with using VBA and userform in Word 2010.

I know almost nothing when it comes to actual programming. I have created a userform using the UI, but it's kind of functionless at this point. The OK and Cancel buttons don't even do anything when clicked.
Here's a picture of the dialog box: https://www.dropbox.com/s/3g4ppnbwsgxbw52/My%20Dialog%20Box.png

Here's the (empty) code:

Private Sub AuthorName_Change()

End Sub

Private Sub AuthorNameLabel_Click()

End Sub

Private Sub CancelButton1_Click()

End Sub

Private Sub CancelButton2_Click()

End Sub

Private Sub Card_Change()

End Sub

Private Sub CardLabel_Click()

End Sub

Private Sub Comment_Change()

End Sub

Private Sub CommentLabel_Click()

End Sub

Private Sub DatePublished_Change()

End Sub

Private Sub DatePublishedLabel_Click()

End Sub

Private Sub FullCitation_Change()

End Sub

Private Sub FullCitationLabel_Click()

End Sub

Private Sub InsertCardButton1_Click()

End Sub

Private Sub InsertCardButton2_Click()

End Sub

Private Sub Institution_Change()

End Sub

Private Sub InstitutionLabel_Click()

End Sub

Private Sub Tag_Change()

End Sub

Private Sub TagLabel_Click()

End Sub

Private Sub Title_Change()

End Sub

Private Sub TitleLabel_Click()

End Sub

Private Sub URL_Change()

End Sub

Private Sub URLLabel_Click()

End Sub


My goal is for the "Insert Card" button to use the input from the dialog box to create a three-lined "card" (using a pre-defined template) right into the document where the blinking cursor is. The "card" template is:

[Tag] ([AuthorName] – [Institution])
[FullCitation][AuthorName] ([Institution]). “[Title].” [DatePublished]. [URL].
[Card]


Everything in brackets would be replaced by the input. Each line has its own style (Line 1 is 'Heading 5', Line 2 is a custom style 'Citation', Line 3 is a custom style 'Evidence'.)

In addition, if there is text entered in the "Comment" field in the dialog box I would like a comment (like Insert-->New Comment) to be created with that input (preferable the comment is selected on the first line of the "card" but its not that important).

Thanks for any and all help!

PS Here's my template in case it's not clear what I have so far:
https://www.dropbox.com/s/wvscdprwhs2rmi7/Dialog%20Box%20Template.dotm

SamT
06-26-2013, 06:03 PM
Here 's some of my thoughts for your Form at this stage (Pre-Code.)

Rename all the TextBox Controls thusly
tbxCard
tbxAuthorName
tbxComment
tbxDatePublished
tbxFullCitation
tbxInstitution
tbxTag
tbxTitle
tbxURL
tbxComment

Add fields to the template and name them thusly.
fldCard
fldAuthorName
fldComment
fldDatePublished
fldFullCitation
fldInstitution
fldTag
fldTitle
fldURL
fldComment

Here are some pseudocode samples for your reading pleasure. (Pseudocode is not code, it is thoughts about code.)

Private Sub cbutCancel1_Click()
'Closes the Form with no other action
Me.Unload
End Sub

Private Sub cbutInsertCard1_Click()
'Enters new Data into Templae,Saves the Templae as NewCardName,
'Then Closes the document and opens the template for the next data set.

'This SubRoutine code might look like this
'First call a function to check for bad or missing entries
If Not VerifyData Then Exit Sub

With Document
fldCard = tbxCard
fldTag = cbxTag
'Etc
End With

'Determine NewCardName
'Document.SaveAs NewCardName
'NewCardName Close
'NewTemplate.Open

End Sub


Private Sub cbutClearAll_Click()
'Clears all the data in the controls without saving it or closing the Form

'This code might look like this

Me.Controls.Value = ""

End Sub

Private Function VerifyData() As Boolean
'Verifies that all tbx's are filled out
'can check and many things as desired.
'Data string lenghts, show MsgBoxes, change BG color of labels,
'move focus to controls, format dates, check for Capitization, Etc
End Function