我正在使用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)
如何解决以上几行中的错误?
答案 0 :(得分:1)
如评论中所述,VBA的代码示例不一定对VB.NET中的代码1:1起作用。对于枚举,有必要限定-代码需要告诉VB.NET在哪里可以找到它们,或者使用其数字(Integer
)等价物。
例如,在声明中您拥有As Word.Table
,它告诉VB.NET Table
是Word
库的成员。对于枚举,需要指定第二级,即枚举的标识符。因此,wdCollapseEnd
枚举值的完全限定为:
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
或者,使用等价的数字:
oRng.Collapse(0)
如果枚举的标识符或数值未知,则可以在“语言参考”或VBA编辑器的对象浏览器中查找(启动Word,F11打开编辑器,F2打开对象浏览器,键入枚举到搜索框中)。
通常,使用完全限定的枚举描述性名称,代码更易读。但是,如果使用后期绑定(不引用Word库),则需要数字值。
答案 1 :(得分:0)
下面是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.")