MS Word VBA。从Footnote字段获取Source对象

时间:2017-12-06 11:34:02

标签: vba ms-word

在我的文件中,我必须为我使用的每个公式添加脚注。例如,我有一些文字添加了参考书目来源

(for examle : E = mc2) 

我有公式

Sub addFootnoteFromSelection()
Selection.MoveRight Unit:=wdCell 'my formula is in table - first row 
                                 'is null. second is actual formula and 
                                 'third contains actual formula's number 
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
With Selection                   'i want footnote mark to be in current 
                                 'formula's number
   With .FootnoteOptions
        .Location = wdBottomOfPage
        .NumberingRule = wdRestartContinuous
        .StartingNumber = 1
        .NumberStyle = wdNoteNumberStyleArabic
   End With
 .Footnotes.Add Range:=Selection.Range, Reference:=""
End With
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.Style = ActiveDocument.Styles("Numer wzoru")
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
End Sub

在那个公式之后我必须添加包含源码的脚注。我决定写一个宏,用选定的参考书目来源为公式添加脚注:

Function stringFromSource(curField As Source) As String
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.LoadXML curField.XML
authors = "":  title = "": publish = "": city = "": year = "": periodic = 
""
'authors
Set surname = xmlDoc.getElementsByTagName("b:Last")
Set name = xmlDoc.getElementsByTagName("b:First")
Dim l As Integer
l = 0
For Each el In surname
    If el.Text = "" Then Exit For
    authors = authors + (el.Text & " " & name(l).Text & "  ")
    l = l + 1
Next el
'title
Set titlex = xmlDoc.getElementsByTagName("b:Title")
For Each el In titlex
    If el.Text = "" Then Exit For
    title = title + (el.Text & " ")
Next el
'publisher
Set pubx = xmlDoc.getElementsByTagName("b:Publisher")
For Each el In pubx
    publish = publish + (el.Text & " ")
Next el
'city
Set cityx = xmlDoc.getElementsByTagName("b:City")
If cityx.Length = 0 Then city = city + ("(brak miasta)" & " ")
For Each el In cityx
    city = city + (el.Text & " ")
Next el
'year
Set yearx = xmlDoc.getElementsByTagName("b:Year")
If yearx.Length = 0 Then year = year + ("(brak roku wydania)" & " ")
For Each el In yearx
    year = year + (el.Text & " ")
Next el
'periodical title
Set periodx = xmlDoc.getElementsByTagName("b:PeriodicalTitle")
For Each el In period
    periodic = periodic + (el.Text & " ")
Next el

Dim outputString As String
outputString = author & "- " & title & ", " & publish & periodic & ", " & 
year
stringFromSource = outputString
End Function

这是有效的,但我希望有完整的源名称(如参考书目领域),而不是引用标记。 我在vba中编写了一个函数,它从Source字段返回正确的查找和格式化字符串:

Sub convertAllFootnotes()
Dim ftn As Footnote
Dim oRng As Range
 For Each ftn In ActiveDocument.Footnotes
    Set oRng = Selection.Range
    oRng.Start = oRng.Start - 1
    oRng.End = oRng.End + 1
    oRng.Select
    oRng.Text = stringFromSource(ftn) 'i don't know how to get source object 
                                      'from footnote
 Next ftn
End Sub

此功能按预期工作。但我想循环遍历所有脚注并使用我的函数将其转换为字符串:

 const data = ['one', 'two', 'three'];

这是我的问题。我不知道如何从脚注中获取Source对象(其中包含Source对象,这不是静态字符串,而是复制了正确的Source字段)

0 个答案:

没有答案