none
how to store a value with font details and write it into a new document? RRS feed

  • Question

  •  I want to store values into variable with font details and write that values into a new document.I don't know how to give font details for the strings.

      
    sub textvalue()  
    dim redtext      
    dim bluetext       
    dim italictext     
    dim str  
    redtext="it is red in colour"   
    bluetext="it is blue in colour"
    italictext="it is italic text"  
    ''I want like italictext.font.colorindex 
    str=redtext+bluetext+italictext  
    writedoc(str)   
    end sub 
        
    function writedoc(str)     
    Dim objWord     
    Dim objDoc        
    Dim objSelection     
    set objWord = createObject("Word.Application") 
    Set objDoc = objWord.Documents.Add 
     objWord.Visible = True  
    Set objSelection = objWord.Selection   
     objSelection.TypeText (str)    
     end function


    Thursday, December 17, 2015 1:26 PM

Answers

All replies

  • Hi VidhyaRaju,

    >> how to store a value with font details and write it into a new document?

    For your requirement, I suggest you use Selection.FormattedText.

    Here is a simple code:

    Sub FS()
     Dim a As Range
     Set a = ActiveDocument.Paragraphs(1).Range
     Dim b As Range
     Set b = ActiveDocument.Paragraphs(2).Range
     Selection.FormattedText = a
     Selection.TypeParagraph
     Selection.FormattedText = b
    End Sub

    You could refer the link below for more information.
    # Selection.FormattedText Property (Word)
    https://msdn.microsoft.com/EN-US/library/office/ff836670.aspx

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, December 18, 2015 6:04 AM
  • No, actually, no content in the document. I have content stored in the variable. I will write that content into a new document. But now I want to write the content with font styles applied. How to do it?
    Friday, December 18, 2015 6:50 AM
  • I'm new to vba so can you explain your code? Actually what is "activedocument.paragraphs(1).range" ? Initially there is no document to take content from and format it. In string itslf i should store font details to be applied and later write it into a document
    Friday, December 18, 2015 6:54 AM
  • Hi VidhyaRaju,

    >> I have content stored in the variable. I will write that content into a new document. But now I want to write the content with font styles applied

    It seems you want to insert content, and then set it font styles, am I right? If so, you could use Range.Font Property.

    Here is a simple code:

    Function writedoc(str)
    Dim objWord
    Dim objDoc
    Dim objSelection
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
     objWord.Visible = True
     Dim r As Range
     Set r = objWord.Selection.Range
     r.Text = str
     r.Font.ColorIndex = wdRed
    End Function

    >> what is "activedocument.paragraphs(1).range" ? Initially there is no document to take content from and format it. In string itslf i should store font details to be applied and later write it into a document

    ActiveDocument.Paragraphs(1).Range will get the first paragraph. But with your requirement, you do not need it.

    For setting font style, you could try the code above.
    # Range.Font Property (Word)
    https://msdn.microsoft.com/en-us/library/office/ff821026.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, December 21, 2015 7:46 AM
  • This question was originally asked on StackOverflow:

    http://stackoverflow.com/questions/34333225/how-to-store-a-value-with-font-details-and-write-it-into-a-new-document


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Monday, December 21, 2015 5:14 PM
    Moderator