我正在开发一个使用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)如何修复以上几行中的错误?
发布于 2019-03-25 14:42:47
正如在一条评论中提到的,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打开对象浏览器,在搜索框中键入枚举)。
通常,使用完全限定枚举描述性名称的代码更具可读性。但是,如果使用后期绑定(没有引用词库),则需要数值。
发布于 2019-03-25 05:52:33
下面是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.")https://stackoverflow.com/questions/55327291
复制相似问题