PDA

View Full Version : [SOLVED:] Find Text with "Title" Style and Insert in UserForm Field



docteam135
03-25-2016, 09:41 AM
Hey Guys,

I have this userform to update the properties of a document. Works super well, and I'm happy with it. But recently I wanted to see if I could get the form to populate the Title field with the text that uses the Title style in the doc.

The Title style is only ever used on one, short line in my docs. Although I probably will end up putting in some kind of error handler in case there are multiple lines right? But I can't get it to insert in the field. So for example, let's say that I paint "My Document" with the "Title" style. If the Title property has not already been set in my doc, I want the Title field in the userform to say "My Document".

Any help with this or the Error Handler would be greatly appreciated.

Just a note, the following code is only the User Initialize section. I do have the OK command button code, but as that's not what I'm having a problem with, I didn't include it. Although I'm more than happy to provide the full code for anyone who's interested :)



Private Sub UserForm_Initialize()
'Populate Category drop-down with options
With cmbCategory
.AddItem "PUBLIC"
.AddItem "INTERNAL"
.AddItem "CONFIDENTIAL"
.AddItem "RESTRICTED"
End With

'Populate Author text field
'If the Author property already has been set, it displays here
If DocPropertyExists("Author") = True Then _
txtAuthor.Value = ActiveDocument.BuiltInDocumentProperties("Author")
'If the Author property is blank, "CompanyName/" displays in the text field
If DocPropertyExists("Author") = False Then _
txtAuthor.Value = "CompanyName/"

'Populate the Subject field with today's date
txtSubject.Value = Format(Now, "mmmm yyyy")

'Populate the Category text field
'If the Category property already has been set, then it displays in the field
If DocPropertyExists("Category") = True Then _
cmbCategory.Value = ActiveDocument.BuiltInDocumentProperties("Category")
'If the Category property is blank, it defaults to PUBLIC
If DocPropertyExists("Category") = False Then _
cmbCategory.Value = cmbCategory.List(0)

'Populate the Title field
'If the Title property already has been set, then it displays in the field
If DocPropertyExists("Title") = True Then _
txtTitle.Value = ActiveDocument.BuiltInDocumentProperties("Title")
'If the Title property is blank AND the Title style is used in the doc
'then the text that has the Title style displays in the field
'THIS IS THE PART THAT DOESN'T WORK
If DocPropertyExists("Title") = False And DocStyleExists("Title") = True Then _
txtTitle.Value = ActiveDocument.Styles("Title")


End Sub


Function DocStyleExists(sDocStyle$) As Boolean
'Is the specified style used in the doc
Dim dpThis As Style
For Each dpThis In ActiveDocument.Styles
If UCase(sDocStyle) = UCase(dpThis.NameLocal) Then
DocStyleExists = True
Exit For
End If
Next
End Function

Function DocPropertyExists(sDocProperty$) As Boolean
'Is the specified property set in the doc
Dim dpThis As DocumentProperty
For Each dpThis In ActiveDocument.BuiltInDocumentProperties
If UCase(sDocProperty) = UCase(dpThis.Name) Then
DocPropertyExists = True
Exit For
End If
Next
End Function

gmaxey
03-25-2016, 02:21 PM
Something like this:


Private Sub UserForm_Initialize()
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = "Title" Then
TextBox1.Text = Left(oPar.Range.Text, Len(oPar.Range.Text) - 1)
Exit For
End If
Next
End Sub

docteam135
03-25-2016, 02:29 PM
YESSSSSSS Ahhh thank you so much!!!

Kate