Word 2007脚本随机字体生成器

时间:2014-06-26 18:45:45

标签: ms-word

任何人都知道或能够帮助我使用Word 2007。 这是一个应该随机设置样式的代码片段。

//Create instance of Word application
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

//Open an existing Word document, Make sure the file is exist in this path- otherwise the program fails.
Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

//Create instance of random object
Set objRandom = CreateObject("System.Random")

intLow = 1
intHigh = 5

//Get the text of word document
Set objRange = objDoc.Range()
//Get the charcters of the text
Set colCharacters = objRange.Characters

 //Set different styles randomly 
For Each strCharacter in colCharacters
    intRandom = objRandom.Next_2(intLow,intHigh)

Select Case intRandom
    Case 1 strCharacter.Font.Name = "Arial"
    Case 2 strCharacter.Font.Name = "Times New Roman"
    Case 3 strCharacter.Font.Name = "Courier New"
    Case 4 strCharacter.Font.Name = "Forte"
End Select

下一步

1 个答案:

答案 0 :(得分:0)

您应该在程序中添加以下引用:

  1. Microsoft.Office.Interop.Word

  2. Microsoft Office对象库

  3. 你可以通过正确的ckick项目来做到这一点 - >添加引用,从GAC选项卡中选择我上面提到的第一个引用,从COM选项卡中选择第二个引用。