首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在单词interop中指定枚举值?

如何在单词interop中指定枚举值?
EN

Stack Overflow用户
提问于 2019-03-25 02:49:13
回答 2查看 534关注 0票数 0

我正在开发一个使用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。由于其保护级别,它可能无法访问

下面是我的代码的摘录

代码语言:javascript
复制
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)

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-25 14:42:47

正如在一条评论中提到的,VBA的代码示例不一定对VB.NET中的代码1:1有效。在枚举的情况下,有必要对它们进行限定-代码需要告诉VB.NET在哪里可以找到它们,或者使用它们的数字(Integer)等价物。

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

代码语言:javascript
复制
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

或者,使用数字等价物:

代码语言:javascript
复制
oRng.Collapse(0)

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

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

票数 1
EN

Stack Overflow用户

发布于 2019-03-25 05:52:33

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

代码语言:javascript
复制
    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.")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55327291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档