添加文档变量的例外(我无法弄清楚)

时间:2018-04-13 07:26:32

标签: vb.net openxml

我无法弄清楚我的错误在哪里.Below是我的代码和例外:

    Imports DocumentFormat.OpenXml
    Imports DocumentFormat.OpenXml.Packaging
    Imports DocumentFormat.OpenXml.Wordprocessing

Public Class Form1

Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Create a word file
    Using wordDocument As WordprocessingDocument = WordprocessingDocument.Create("C:\Users\local\Desktop\vb testing purpose\test.docx", WordprocessingDocumentType.Document)
        ' Add a main document part. 
        Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
        ' Create the document structure and add some text.
        mainPart.Document = New Document()

        Dim body As Body = mainPart.Document.AppendChild(New Body())
        Dim para As Paragraph = body.AppendChild(New Paragraph())
        Dim run As Run = para.AppendChild(New Run())
        run.AppendChild(New Text(""))
    End Using
    Dim strDoc As String = "C:\Users\local\Desktop\vb testing purpose\test.docx"
    Dim varname As String = "Hi"
    Dim varvalue As String = "Bryan"
    AddDocVar(strDoc, varname, varvalue)
   End Sub

   Public Sub AddDocVar(ByVal sender As Object, ByVal varName As String, ByVal varValue As String)
   Dim mainPart As MainDocumentPart = sender.MainDocumentPart
    Dim settingsPart As DocumentSettingsPart = mainPart.DocumentSettingsPart
    settingsPart.Settings = New Settings(New DocumentVariables(New DocumentVariable() With {.Name = varName, .Val = varValue}))

End Sub
 End Class

出来

  

例外(System.MissingMemberException:'公共成员' MainDocumentPart'类型'字符串'未找到。')

Dim mainPart As MainDocumentPart = sender.MainDocumentPart

0 个答案:

没有答案