Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert text an format it with varius styles.

Status
Not open for further replies.

into

Technical User
Dec 16, 2002
17
I would be glad if any of u can help me to get on track.

I'm bylding a template that will insert text at the end of the doument but I have stuck om how to add style to the text that i inserted.

The text are varius headers and standard text.

plz reply asap

thnx
into_se
 
Have you looked-up 'style' in the vbe help? The examples there include one that shows how to apply a style to a paragraph.

Cheers

[MS MVP - Word]
 
Thats not the problem. i know how to simply apply a style, what I want to know is how I should insert the text and apply the styles of choice in a good function.

/into_se
 
i know how to simply apply a style, what I want to know is how I should insert the text and apply the styles of choice in a good function.
That does not make sense.

1. You know how to apply a style
2. You know how to insert text (as you state in your original posted - " the text that i inserted.")

Uh....put the two of them together. Insert the text, and apply the style. You say you know how to do both, so I don't understand the problem.

Perhaps if you tell us HOW you are inserting the text it may help. Are you inserting it as a string? As the text of a range? As typed in text? In any case, once inserted, it is a paragraph like any other, and can have a style attached to it.

Otherwise..well, look in Help. It is pretty clear there.

Gerry
My paintings and sculpture
 
Ok, i understand what you mean, but its only på pour english that is the problem here.

I'll try to explain it again with more details.

I have a UserForm witch i start with a menu button, in that form i have several controls like textfield and stuf that updates bookmarks on the page in the header and footer, this is working fine.

What i am now trynig to acomplish is to har several option buttons that I want the user to select witch kind of documents they want to create and it will insert the text by there selection.

An example.

At a specific bookmark insert following......




Projectdescription (Header 3)
vbCrLf <emptyline> (Normal)
vbCrLf <emptyline> (Normal)
Projektplan (Header 3)
vbCrLf <emptyline> (Normal)
vbCrLf <emptyline> (Normal)

and so forth

the number of headers is dynamic.

Hope i have cleard som questions

i know can read the f****** manual but i think there is a better way to do this in good code.

so plz help









 
i know can read the f****** manual but i think there is a better way to do this in good code.
I will gladly offer a better way...when you state what way you ARE doing. So far, you are not saying what way you are doing...so how can I suggest a BETTER way?

Post what code you are trying, or thinking of trying, and if there is a better way (and probably there is), I will happily offer suggestions.

Further, you will get better help if, when someone asks specific questions....you actually bother to answer them.
At a specific bookmark insert following......

Projectdescription (Header 3)
vbCrLf <emptyline> (Normal)
vbCrLf <emptyline> (Normal)
Projektplan (Header 3)
vbCrLf <emptyline> (Normal)
vbCrLf <emptyline> (Normal)
AGAIN...is this going to be inserted as a string, or were you planning to use TypeText? It makes a difference. Do you want to be able to re-use the bookmark? Were you just going to use an InsertAfter? In which case the bookmarks itself will not contain the text, and you would have to reselect the inserted text in order to apply the styles.

I can post code right now that will insert any text you want into any bookmark you want, and keep it within the bookmark range, which means you can easily apply anything you want (including styles) to the bookmark range itself by paragraph.

But...I certainly am not going to, as I have no idea what you ARE doing so far. So post your code.

Gerry
My paintings and sculpture
 
What is TypeText?
I think it's a string i am inserting.
I think i would like to insert the text after the bookmark, so i think i need to reselect as u suggest.



This is what i would like to do, know it will not work as it is but i hope i'll get some guidence by you.

Code:
Selection.InsertAfter "Projectname"
Selection.Style = ActiveDocument.Styles("Header 1")

Selection.InsertAfter vbCrLf 
Selection.Style = ActiveDocument.Styles("Normal")

Selection.InsertAfter vbCrLf 
Selection.Style = ActiveDocument.Styles("Normal")

Selection.InsertAfter "Projectdesctiption" 
Selection.Style = ActiveDocument.Styles("Header 3")


This is my code at this time

Code:
'-------------------------------------------------
'Vid start av formulär
'-------------------------------------------------
Private Sub UserForm_Initialize()
    
    Dim sBMCustomer, sBMContact, sBMDate, sBMProject, sBMResponsible
    
    If ActiveDocument.Bookmarks.Exists("Kund") = True Then
        sBMCustomer = ActiveDocument.Bookmarks("Kund").Range.Text
    End If
    
    If ActiveDocument.Bookmarks.Exists("Kontakt") = True Then
        sBMContact = ActiveDocument.Bookmarks("Kontakt").Range.Text
    End If
    If ActiveDocument.Bookmarks.Exists("Datum") = True Then
        sBMDate = ActiveDocument.Bookmarks("Datum").Range.Text
    End If
    If ActiveDocument.Bookmarks.Exists("Projektnamn") = True Then
        sBMProject = ActiveDocument.Bookmarks("Projektnamn").Range.Text
    End If
    If ActiveDocument.Bookmarks.Exists("Ansvarig") = True Then
        sBMResponsible = ActiveDocument.Bookmarks("Ansvarig").Range.Text
    End If
    If ActiveDocument.Bookmarks.Exists("Rubrik") = True Then
        sBMHeader = ActiveDocument.Bookmarks("Rubrik").Range.Text
    End If
    
    Me.txtCustomer = sBMCustomer
    Me.txtContact = sBMContact
    Me.txtHeader = sBMHeader
    Me.txtDate = sBMDate
    Me.txtProject = sBMProject
    Me.txtResponsible = sBMResponsible
End Sub


Private Sub cbok_Click()
    Application.ScreenUpdating = False
        
    'Om bokmärke finns uppdatera
        If ActiveDocument.Bookmarks.Exists("Kund") = True Then
        UpdateBookmark "Kund", txtCustomer.Value
    End If
        
    If ActiveDocument.Bookmarks.Exists("Kontakt") = True Then
        UpdateBookmark "Kontakt", txtContact.Value
    End If
        
    If ActiveDocument.Bookmarks.Exists("Datum") = True Then
        UpdateBookmark "Datum", txtDate.Value
    End If
        
     If ActiveDocument.Bookmarks.Exists("Projektnamn") = True Then
        UpdateBookmark "Projektnamn", txtProject.Value
    End If
        
    If ActiveDocument.Bookmarks.Exists("Ansvarig") = True Then
        UpdateBookmark "Ansvarig", txtResponsible.Value
    End If
        
    If ActiveDocument.Bookmarks.Exists("Rubrik") = True Then
        UpdateBookmark "Rubrik", txtHeader.Value
    End If
        
    
    Application.ScreenUpdating = True
    
    If ActiveDocument.Bookmarks.Exists("Start") = True Then
       Selection.GoTo What:=wdGoToBookmark, Name:="Start"
    End If
    
    Unload Me
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


'-------------------------------------------------
'Visa eget formulär
'-------------------------------------------------
Sub ShowForm()
    frmInto.Show
End Sub


'-------------------------------------------------
'Visa Spara som formulär
'-------------------------------------------------
Sub ShowSaveAsForm()
Dim sBMProject, sBMDate
sBMProject = ActiveDocument.Bookmarks("Projektnamn").Range.Text
sBMDate = ActiveDocument.Bookmarks("Datum").Range.Text
With Dialogs(wdDialogFileSaveAs)
    .Name = sBMProject & " " & sBMDate
    .Show
End With
End Sub


'-------------------------------------------------
'Ändra storlek på skärmdump till 65%
'-------------------------------------------------
Sub RezisePicture()
Application.ScreenUpdating = False

If Selection.Type = wdSelectionInlineShape Then

With Selection.InlineShapes(1)
    .ScaleHeight = 65
    .ScaleWidth = 65
End With

End If
Application.ScreenUpdating = True
End Sub

I hope i have made my self clear.

/Torfi
 
Yes, thank you. Now we have something to actually work with.

1. You can make your code a bit more efficient with the checking if the bookmarks exist, then getting the Range.Text. Use an array.
Code:
Dim BMArray(5) As String
Dim BMText(5) As String
Dim var
BMArray(0) = "Kund"
BMArray(1) = "Kontakt"
BMArray(2) = "Datum"
BMArray(3) = "Projektnamn"
BMArray(4) = "Ansvarig"
BMArray(5) = "Rubrik"
For var = 0 To UBound(BMArray())
 If ActiveDocument.Bookmarks.Exists(BMArray(var)) = True Then
    BMText(var) = ActiveDocument.Bookmarks(BMArray(var)).Range.Text
Next
    Me.txtCustomer = BMText(0)
    Me.txtContact = BMText(1)
    Me.txtHeader = BMText(2)
    Me.txtDate = BMText(3)
    Me.txtProject = BMText(5)
    Me.txtResponsible = BMText(5)
uses an array of the bookmarks to check if each exists, and if it does, fill a matching array of the text. You don't need to do separate IF statements.

2. You do not say, but it LOOKS like you want to do your text insertion at
Code:
    If ActiveDocument.Bookmarks.Exists("Start") = True Then
       Selection.GoTo What:=wdGoToBookmark, Name:="Start"
    End If
You really should comment that. I can only guess that it where you want to do the text insertion, because you are using the Selection - see following.

3. The code
Code:
Selection.InsertAfter "Projectname"
Selection.Style = ActiveDocument.Styles("Header 1")

Selection.InsertAfter vbCrLf 
Selection.Style = ActiveDocument.Styles("Normal")

Selection.InsertAfter vbCrLf 
Selection.Style = ActiveDocument.Styles("Normal")

Selection.InsertAfter "Projectdesctiption" 
Selection.Style = ActiveDocument.Styles("Header 3")
is backwards. You make the Selection style whatever you want, THEN put the text. Let's look at it.

Projectname - paragraph 1 (uses the first vbcrlf)
blank paragraph - paragraph 2 (uses the second vbcrlf)
Projectdesctiption - paragraph 3

Again, you don't say, but if you ARE, or CAN, use the bookmark Start itself, then
Code:
Dim strTextInsert As String
Dim r As Word.Range
[COLOR=red]' your string[/color red]
strTextInsert = "Projectname" & vbCrLf & vbCrLf & _
   "Projectdesctiption"

UpdateBookmark "Start", strTextInsert
Set r = ActiveDocument.Bookmarks("Start").Range
r.Paragraphs(1).Range.Style = "Heading 3"
r.Paragraphs(3).Range.Style = "Heading 3"
Set r = Nothing
There are other ways...but you are not fully clear on where the text is to be inserted, and it may change depending on that.

Hope this helps.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top