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?
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]