VBA format text in word

BaseBallBatBoy

New Member
Joined
Sep 12, 2011
Messages
24
Hi,

So far I filled a txt file out of Excel 2007 VBA. Now I would like to push the same text into a word file. Just the text itself works fine, but now comes formatting, which I don't really understand how it works. Here is what I've done so far:

Code:
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Add ' create a new document
    
    With wrdDoc
        .Styles(wdStyleHeading1).Font.Name = "Arial"
        .Styles(wdStyleHeading1).Font.Size = 16
        .Styles(wdStyleHeading1).Font.Bold = True
        .Styles(wdStyleHeading1).Font.Color = wdColorBlack
        
        .Styles(wdStyleHeading2).Font.Name = "Arial"
        .Styles(wdStyleHeading2).Font.Size = 12
        .Styles(wdStyleHeading2).Font.Bold = True
        .Styles(wdStyleHeading2).Font.Color = wdColorBlack
        
        .Styles(wdStyleNormal).Font.Name = "Arial"
        .Styles(wdStyleNormal).Font.Size = 10
        .Styles(wdStyleNormal).Font.Color = wdColorBlack
    
        .Content.ParagraphFormat.LineSpacingRule = wdLineSpaceExactly
        .Content.ParagraphFormat.LineSpacing = 10

        .Content.InsertAfter "THIS SHOULD BE HEADING1"
        .Content.InsertParagraphAfter

        .Content.InsertAfter "THIS SHOULD BE HEADING2"
        .Content.InsertParagraphAfter

        .Content.InsertAfter "THIS SHOULD BE NORMAL"
        .Content.InsertParagraphAfter
        
        .SaveAs (path)
        .Close ' close the document
    End With    ' With wrdDoc
    
    wrdApp.Quit ' close the Word application
    Set wrdDoc = Nothing
    Set wrdApp = Nothing

Now, how can I apply a certain style to a text I just inserted before? I believe it's self-explanatory out of my code which text needs to have which style.

Regards,
BBBB
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
<font face=Courier New>    <SPAN style="color:#00007F">Dim</SPAN> wrdApp <SPAN style="color:#00007F">As</SPAN> Word.Application<br>    <SPAN style="color:#00007F">Dim</SPAN> wrdDoc <SPAN style="color:#00007F">As</SPAN> Word.Document<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> wrdApp = CreateObject("Word.Application")<br>    <SPAN style="color:#00007F">Set</SPAN> wrdDoc = wrdApp.Documents.Add <SPAN style="color:#007F00">' create a new document</SPAN><br>    wrdApp.Visible = <SPAN style="color:#00007F">True</SPAN><br>    <br>    <SPAN style="color:#00007F">With</SPAN> wrdDoc<br>        <br>        <SPAN style="color:#00007F">With</SPAN> .Styles(wdStyleHeading1).Font<br>            .Name = "Arial"<br>            .Size = 16<br>            .Bold = <SPAN style="color:#00007F">True</SPAN><br>            .Color = wdColorBlack<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> .Styles(wdStyleHeading2).Font<br>            .Name = "Arial"<br>            .Size = 12<br>            .Bold = <SPAN style="color:#00007F">True</SPAN><br>            .Color = wdColorBlack<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> .Styles(wdStyleNormal).Font<br>            .Name = "Arial"<br>            .Size = 10<br>            .Color = wdColorBlack<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br>        .Content.ParagraphFormat.LineSpacingRule = wdLineSpaceExactly<br>        .Content.ParagraphFormat.LineSpacing = 10<br><br>        .Range(0).Style = .Styles(wdStyleHeading1)<br>        .Content.InsertAfter "THIS SHOULD BE HEADING1"<br>        .Content.InsertParagraphAfter<br>        <br>        .Range(.Characters.count - 1).Style = .Styles(wdStyleHeading2)<br>        .Content.InsertAfter "THIS SHOULD BE HEADING2"<br>        .Content.InsertParagraphAfter<br><br>        .Range(.Characters.count - 1).Style = .Styles(wdStyleNormal)<br>        .Content.InsertAfter "THIS SHOULD BE NORMAL"<br>        .Content.InsertParagraphAfter<br>                <br>        <SPAN style="color:#007F00">'.SaveAs (Path)</SPAN><br>        <SPAN style="color:#007F00">'.Close ' close the document</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>    <SPAN style="color:#007F00">' With wrdDoc</SPAN><br>    <br>    <SPAN style="color:#007F00">'wrdApp.Quit ' close the Word application</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wrdDoc = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wrdApp = <SPAN style="color:#00007F">Nothing</SPAN></FONT>


Example MS-Word code
 
Upvote 0
Another problem showed up. I write some text and after that I want to insert a page break. But everything I wrote before the brake will be erased and replaced by just the page break itself. How can I keep the texts?

Code:
Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Add ' create a new document
    
With wrdDoc
        .Range(0).Style = .Styles(wdStyleHeading1)
        .Content.InsertAfter "PAGE1"
        .Content.InsertParagraphAfter
        
        .Content.InsertAfter "Some text here"
        .Content.InsertParagraphAfter
        
        .Content.InsertBreak Type:=wdPageBreak
        
        .Range(.Characters.Count - 1).Style = .Styles(wdStyleHeading1)
        .Content.InsertAfter "PAGE2"
        .Content.InsertParagraphAfter

        ....and so on.....
End With

With this example there would only be a page break and then "PAGE2". Any ideas? Where is my error in reasoning?
 
Upvote 0
This code was from the VBA help. Highlight .InsertBreak in your VBA code and press F1.

<font face=Courier New>    <SPAN style="color:#00007F">Set</SPAN> wrdApp = CreateObject("Word.Application")<br>    <SPAN style="color:#00007F">Set</SPAN> wrdDoc = wrdApp.Documents.Add <SPAN style="color:#007F00">' create a new document</SPAN><br>    wrdApp.Visible = <SPAN style="color:#00007F">True</SPAN><br>        <br>    <SPAN style="color:#00007F">With</SPAN> wrdDoc<br>            .Range(0).Style = .Styles(wdStyleHeading1)<br>            .Content.InsertAfter "PAGE1"<br>            .Content.InsertParagraphAfter<br>            <br>            .Content.InsertAfter "Some text here"<br>            .Content.InsertParagraphAfter<br>            <br>            <SPAN style="color:#00007F">With</SPAN> .Paragraphs(.Paragraphs.Count).Range<br>                .Collapse Direction:=wdCollapseEnd<br>                .InsertBreak Type:=wdPageBreak<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            <br>            .Range(.Characters.Count - 1).Style = .Styles(wdStyleHeading1)<br>            .Content.InsertAfter "PAGE2"<br>            .Content.InsertParagraphAfter<br>    <br>            <SPAN style="color:#007F00">'....and so on.....</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN></FONT>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,919
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top