VBA Word Document from Excel - Can't figure out how to format Word text

bstaaa

New Member
Joined
Dec 29, 2017
Messages
7
I've created a macro to build custom word documents templates for me based on Excel data (and hundreds are made). The creation of the documents works, but for the life of me, I can't find out how to individually format portions of the text I insert.

As background, the code uses a table like the one below to create said word documents.


[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name (i.e. Unique Identifier)[/TD]
[TD]Revision #[/TD]
[TD]Summary[/TD]
[/TR]
[TR]
[TD]12345[/TD]
[TD]4[/TD]
[TD]Proposal for Project A[/TD]
[/TR]
[TR]
[TD]45674[/TD]
[TD]8[/TD]
[TD]Proposal for Project B[/TD]
[/TR]
</tbody>[/TABLE]


Please take a look at the code below, how do we format certain portions of text?

Code:
[COLOR=#212121][FONT=Calibri]Sub Create()[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]Dim i As Integer[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]Dim j As Integer[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]Dim Name As String[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]Dim Revision As Integer [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]  [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]           [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]     [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        For i = 1 To Cells(1, 1).End(xlDown).Row[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        With CreateObject("Word.Document")[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Windows(1).Visible = False[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        Name = Cells(i, 1)[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        Revision= Cells(i, 2)[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        summary= Cells(i, 3)[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Styles.Add ("HdrText")[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        With .Styles("HdrText").Font[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            .Name = "Arial"[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            .Size = 14[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            .Bold = True[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            .Underline = False[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        End With[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]       [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.InsertAfter "Invoice" + Name[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Selection.TypeParagraph[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.InsertParagraphAfter[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.InsertAfter Summary[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.InsertParagraphAfter[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.ParagraphFormat.Alignment = wdAlignParagraphLeft[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.InsertAfter "Introduction text here"[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.InsertParagraphAfter[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.InsertAfter "The following revision actions have been done:"[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Content.InsertParagraphAfter[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]      [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            For j = 1 To revision[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]           [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]                .Content.InsertAfter "Revision #" & j[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]                .Content.InsertParagraphAfter[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]           [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            Next j[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]       [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            If Dir("C:\Downloads\Test\" + Name + " - Revision Summary") <> "" Then[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            Kill "C:\Downloads\Test\" + Name + " - Revision Summary"[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]            End If[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]       [/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .SaveAs Filename:=("C:\Users\BMSTARK\Downloads\Test\" + Name + " - Revision Summary")[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]        .Close Savechanges = False  ' close the document[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]    End With[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]    Next i[/FONT][/COLOR]
[COLOR=#212121][FONT=Calibri]End Sub[/FONT][/COLOR]
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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