从跟踪的更改中删除时间戳

问题描述 投票:0回答:0
Sub Change_Date_Time()

    Dim sWOOXML As String
    
    Set objRegEx = CreateObject("vbscript.regexp")
    objRegEx.Global = True
    objRegEx.IgnoreCase = True
    objRegEx.MultiLine = True
    
    objRegEx.Pattern = "w:date=""\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{1,2}:\d{1,2}Z"""

    sWOOXML = ActiveDocument.Content.WordOpenXML
    sWOOXML = objRegEx.Replace(sWOOXML, "")
        
    ActiveDocument.Content.InsertXML sWOOXML
    
    Beep
    
End Sub

以上宏使用正则表达式对象从 Word 中的跟踪更改中删除时间戳。但是,正则表达式方法不适用于 MacOS。因此,我试图修改宏以遍历时间戳的所有节点并将它们删除。以下代码未按预期工作:

Sub Loop_Through_Dates()

    Dim sWOOXML As String
    Dim xmlDoc As MSXML2.DOMDocument60
    Dim xmlNodeList As MSXML2.IXMLDOMNodeList
    Dim xmlNode As MSXML2.IXMLDOMNode
    
    Set xmlDoc = New MSXML2.DOMDocument60
    
    sWOOXML = ActiveDocument.Content.WordOpenXML
    
    xmlDoc.LoadXML sWOOXML
    
    'Error Reference to undeclared namespace prefix: 'w'
    Set xmlNodeList = xmlDoc.SelectNodes("//w:date") 'here not addressing the right node
    
    For Each xmlNode In xmlNodeList
        Debug.Print xmlNode.text 'need to remove them
    Next xmlNode
    
End Sub

XML

<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:oel="h
//schemas.microsoft.com/office/2019/extlst" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg=
p://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
<w:body>
<w:p w14:paraId="28EE5C22" w14:textId="2E0003B7" w:rsidR="00BC5D48" w:rsidRDefault="00BC5D48" w:rsidP="00E36D9B">
<w:pPr>
<w:contextualSpacing/>
<w:jc w:val="center"/>
<w:rPr>
<w:ins w:id="0" w:author="Emily Shepherd" w:date="2022-06-03T12:03:00Z"/>
<w:b/>
<w:sz w:val="36"/>
<w:szCs w:val="36"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w14:paraId="519B4531" w14:textId="77777777" w:rsidR="00AA3C3F" w:rsidRPr="00BC5D48" w:rsidRDefault="00AA3C3F" w:rsidP="00E36D9B">
<w:pPr>
<w:contextualSpacing/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:sz w:val="36"/>
<w:szCs w:val="36"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w14:paraId="4ECF5332" w14:textId="6D351833" w:rsidR="001E42F1" w:rsidRPr="001E42F1" w:rsidRDefault="00E36D9B" w:rsidP="00E36D9B">
<w:pPr>
<w:contextualSpacing/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:sz w:val="50"/>
<w:szCs w:val="50"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:b/>
<w:sz w:val="50"/>
<w:szCs w:val="50"/>
</w:rPr>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:r w:rsidR="001E42F1">
<w:rPr>
<w:b/>
<w:sz w:val="50"/>
<w:szCs w:val="50"/>
</w:rPr>
<w:t>Introduction</w:t>
</w:r>
</w:p>
<w:p w14:paraId="4F162E16" w14:textId="10EC54DC" w:rsidR="001E42F1" w:rsidRDefault="00E36D9B" w:rsidP="001E42F1">
<w:pPr>
<w:contextualSpacing/>
<w:rPr>
<w:b/>
<w:sz w:val="30"/>
<w:szCs w:val="30"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:b/>
<w:noProof/>
<w:sz w:val="30"/>
<w:szCs w:val="30"/>
</w:rPr>
<mc:AlternateContent>
<mc:Choice Requires="wps">
<w:drawing>
<wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251659264" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1" wp14:anchorId="55802E59" wp14:editId="0E728869">
<wp:simplePos x="0" y="0"/>
<wp:positionH relativeFrom="column">
<wp:posOffset>1644162</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="paragraph">
<wp:posOffset>34437</wp:posOffset>
</wp:positionV>
<wp:extent cx="2672666" cy="0"/>
<wp:effectExtent l="0" t="12700" r="20320" b="25400"/>
<wp:wrapNone/>
<wp:docPr id="1" name="Straight Connector 1"/>
<wp:cNvGraphicFramePr/>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<wps:wsp>
<wps:cNvCnPr/>
<wps:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="2672666" cy="0"/>
</a:xfrm>
<a:prstGeom prst="line">
<a:avLst/>
</a:prstGeom>
<a:effectLst>
<a:outerShdw dist="20000" sx="1000" sy="1000" rotWithShape="0">
<a:srgbClr val="000000"/>
</a:outerShdw>
</a:effectLst>
</wps:spPr>
<wps:style>
<a:lnRef idx="2">
<a:schemeClr val="dk1"/>
</a:lnRef>
<a:fillRef idx="0">
<a:schemeClr val="dk1"/>
</a:fillRef>
<a:effectRef idx="1">
<a:schemeClr val="dk1"/>
</a:effectRef>
<a:fontRef idx="minor">
<a:schemeClr val="tx1"/>
</a:fontRef>
</wps:style>
<wps:bodyPr/>
</wps:wsp>
</a:graphicData>
</a:graphic>
<wp14:sizeRelH relativeFrom="margin">
<wp14:pctWidth>0</wp14:pctWidth>
</wp14:sizeRelH>
</wp:anchor>
</w:drawing>
</mc:Choice>
<mc:Fallback>
<w:pict>

更新 尽管下面的代码似乎完全没有错误,但文档中没有显示期望的结果(期望的结果是删除时间戳):

Sub RemoveAttribute()

    Dim xmlDoc As Object
    Set xmlDoc = CreateObject("MSXML2.DOMDocument")
    xmlDoc.async = False
    xmlDoc.validateOnParse = False
    
    ' Get the XML of the active document
    xmlDoc.LoadXML ActiveDocument.Content.WordOpenXML 'ActiveDocument.Content.xml
    
    If xmlDoc.parseError.ErrorCode <> 0 Then
        MsgBox "Error in XML document: " & xmlDoc.parseError.reason
        Exit Sub
    End If

    ' Get all the "w:ins" elements in the document
    Dim insNodes As Object
    Set insNodes = xmlDoc.SelectNodes("//w:ins")

    Dim insNode As Object
    For Each insNode In insNodes
        ' Remove the "w:date" attribute from each "w:ins" element
        Debug.Print insNode.getAttribute("w:date")
        insNode.RemoveAttribute ("w:date")
        
    Next insNode
    
    ' Insert the modified XML back into the active document
    Dim sModifiedXML As String
    sModifiedXML = xmlDoc.xml
    ActiveDocument.Content.InsertXML sModifiedXML
    
End Sub
regex xml vba ms-word openxml-sdk
© www.soinside.com 2019 - 2024. All rights reserved.