如何在 Word 互操作中指定枚举值?



我正在开发一个工具,使用 Visual Basic .NET 在 Word 文档中自动编辑。我已经引用了Word库并导入了Microsoft.Office.Interop命名空间。

由于我是初学者,我首先在以下链接(https://support.microsoft.com/en-us/help/313193/how-to-automate-microsoft-word-by-using-visual-basic-to-create-a-new-d)中运行示例。但是,此链接中的代码已过时,我检查的所有引用都无助于修复将参数分配给枚举方法,它给出了以下错误

wdcollapseEnd 未声明。它可能无法访问,因为它 保护级别

这是我的代码摘录

Do 
oRng = oDoc.Bookmarks("endofdoc").Range
oRng.ParagraphFormat.SpaceAfter = 6
oRng.InsertAfter("A line of text")
oRng.InsertParagraphAfter()
Loop While Pos >= oRng.Information(wdVerticalPositionRelativeToPage)
'wdVerticalPositionRelativeToPage
oRng.Collapse(wdCollapseEnd)
oRng.InsertBreak(wdpagebreak)

如何修复以上几行中的错误?

如注释中所述,VBA 的代码示例不一定适用于 VB.NET 中的代码 1:1。在枚举的情况下,有必要限定它们 - 代码需要告诉 VB.NET 在哪里找到它们,或者使用它们的数值(Integer)等价。

例如,在声明中,您As Word.Table,它告诉 VB.NETTableWord库的成员。对于枚举,需要指定第二个级别,即枚举的标识符。因此,wdCollapseEnd枚举值的完整限定是:

oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

或者,使用等效数值:

oRng.Collapse(0)

如果枚举的标识符或数值未知,则可以在语言参考或 VBA 编辑器的对象浏览器中查找它(启动 Word,按 F11 打开编辑器,按 F2 打开对象浏览器,在搜索框中键入枚举)。

通常,使用完全限定的枚举描述性名称的代码更具可读性。但是,如果使用后期绑定(不引用 Word 库),则需要数值。

下面是Visual Basic示例代码的工作版本,它使用枚举值而不是名称:

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
Dim oRng As Word.Range
Dim oShape As Word.InlineShape
Dim oChart As Object
Dim Pos As Double

'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
'Insert a paragraph at the beginning of the document.
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = "Heading 1"
oPara1.Range.Font.Bold = True
oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter()
'Insert a paragraph at the end of the document.
'** endofdoc is a predefined bookmark.
oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("endofdoc").Range)
oPara2.Range.Text = "Heading 2"
oPara2.Format.SpaceAfter = 6
oPara2.Range.InsertParagraphAfter()
'Insert another paragraph.
oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("endofdoc").Range)
oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"
oPara3.Range.Font.Bold = False
oPara3.Format.SpaceAfter = 24
oPara3.Range.InsertParagraphAfter()
'Insert a 3 x 5 table, fill it with data and make the first row
'bold,italic.
Dim r As Integer, c As Integer
oTable = oDoc.Tables.Add(oDoc.Bookmarks("endofdoc").Range, 3, 5)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 3
For c = 1 To 5
oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
oTable.Rows(1).Range.Font.Bold = True
oTable.Rows(1).Range.Font.Italic = True
'Add some text after the table.
'oTable.Range.InsertParagraphAfter
oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("endofdoc").Range)
oPara4.Range.InsertParagraphBefore()
oPara4.Range.Text = "And here's another table:"
oPara4.Format.SpaceAfter = 24
oPara4.Range.InsertParagraphAfter()
'Insert a 5 x 2 table, fill it with data and change the column widths.
oTable = oDoc.Tables.Add(oDoc.Bookmarks("endofdoc").Range, 5, 2)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 5
For c = 1 To 2
oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
oTable.Columns(1).Width = oWord.InchesToPoints(2)   'Change width of columns 1 & 2.
oTable.Columns(2).Width = oWord.InchesToPoints(3)
'Keep inserting text. When you get to 7 inches from top of the
'document, insert a hard page break.
Pos = oWord.InchesToPoints(7)
oDoc.Bookmarks("endofdoc").Range.InsertParagraphAfter()
Do
oRng = oDoc.Bookmarks("endofdoc").Range
oRng.ParagraphFormat.SpaceAfter = 6
oRng.InsertAfter("A line of text")
oRng.InsertParagraphAfter()
Loop While Pos >= oRng.Information(6)
'wdVerticalPositionRelativeToPage
oRng.Collapse(0)
oRng.InsertBreak(7)
oRng.Collapse(0)
oRng.InsertAfter("We're now on page 2. Here's my chart:")
oRng.InsertParagraphAfter()
'Insert a chart and change the chart.
oShape = oDoc.Bookmarks("endofdoc").Range.InlineShapes.AddOLEObject(
ClassType:="MSGraph.Chart.8", FileName _
:="", LinkToFile:=False, DisplayAsIcon:=False)
oChart = oShape.OLEFormat.Object
oChart.charttype = 4 'xlLine = 4
oChart.Application.Update
oChart.Application.Quit
'... If desired, you can proceed from here using the Microsoft Graph 
'Object model on the oChart object to make additional changes to the
'chart.
oShape.Width = oWord.InchesToPoints(6.25)
oShape.Height = oWord.InchesToPoints(3.57)
'Add text after the chart.
oRng = oDoc.Bookmarks("endofdoc").Range
oRng.InsertParagraphAfter()
oRng.InsertAfter("THE END.")

最新更新