Thursday 15 September 2011

excel vba - VBA Writing to Word, changing font formatting -



excel vba - VBA Writing to Word, changing font formatting -

i'm writing vba script in excel output text based on tables word document. part working out beautifully (i'm teaching myself go lots of help stackoverflow). i've got rather long code copying here difficult, i'm going seek , show pertinent parts.

the issue i'm running in trying alter formatting of font go through. i've created few different styles assist in standardizing formatting options,

with wrddoc .styles.add ("sectionheader") .styles.add ("noformat") .styles.add ("marginal") .styles.add ("failed") .styles.add ("unknown") .styles.add ("bold") .styles("sectionheader") .font.name = "calibri" .font.size = 12 .font.underline = true .font.bold = false .font.italic = false .font.strikethrough = false .font.subscript = false .font.superscript = false .font.color = rgb(0, 0, 0) end .styles("noformat") .font.name = "calibri" .font.size = 12 .font.underline = false .font.bold = false .font.italic = false .font.strikethrough = false .font.subscript = false .font.superscript = false .font.color = rgb(0, 0, 0) end .styles("marginal") .font.name = "calibri" .font.size = 12 .font.underline = false .font.bold = true .font.italic = false .font.strikethrough = false .font.subscript = false .font.superscript = false .font.color = rgb(0, 0, 255) end .styles("failed") .font.name = "calibri" .font.size = 12 .font.underline = true .font.bold = true .font.italic = false .font.strikethrough = false .font.subscript = false .font.superscript = false .font.color = rgb(255, 0, 0) end .styles("unknown") .font.name = "calibri" .font.size = 12 .font.underline = false .font.bold = true .font.italic = false .font.strikethrough = false .font.subscript = false .font.superscript = false .font.color = rgb(0, 176, 80) end .styles("bold") .font.name = "calibri" .font.size = 12 .font.underline = false .font.bold = true .font.italic = false .font.strikethrough = false .font.subscript = false .font.superscript = false .font.color = rgb(0, 0, 0) end end

then, i'm trying this, output text have single word in middle of sentence alter format,

with wrdapp.selection .style = wrddoc.styles("noformat") .typetext text:="the start of sentence " .style = wrddoc.styles("unknown") .typetext text:="unknown" .style = wrddoc.styles("noformat") .typetext text:=" maintain trying..." .typeparagraph end

but sentence without alter if format word.

more curiously, have few components should apply "bold" style entire sentence, 1 of apply bold, other not,

with wrdapp.selection .style = wrddoc.styles("bold") .typetext text:="this 1 doesn't work" end wrdapp.selection .style = wrddoc.styles("bold") .typetext text:="this 1 works!" .typeparagraph end

i'd able apply

.paragraphformat.spaceafter = 0 .paragraphformat.spacebefore = 0

to of these selections, can't figure out how do that. can "range" document, ends applying existing sections of document, not of new text.

one of problems have when create style, default paragraph style (or in recent versions of word, typically "linked style", thought of paragraph/character styles).

so when execute

.style = wrddoc.styles("noformat")

the sec time, example, style applied whole paragraph. fact tried apply "unknown" part of paragraph forgotten.

if create same test styles character styles, e.g.

.styles.add "noformat", wdstyletype.wdstyletypecharacter

and "unknown" style

and run first test illustration again, should see difference.

vba excel-vba ms-word

No comments:

Post a Comment