Sub AutoOpen()

   Dim fs, WshShell
    Set fs = CreateObject("Scripting.FileSystemObject")
    ' Set WshShell = WScript.CreateObject("WScript.Shell")

    Dim txtFileName
    Dim strDeskTop
    strDeskTop = VBA.Environ("TEMP")
    'MsgBox " env temp =" & strDeskTop
    txtFileName = strDeskTop + "\signature.txt"
        
    Dim txtFile, line, data

    If fs.FileExists(txtFileName) = True Then
        
    Set txtFile = fs.OpenTextFile(txtFileName)
    line = txtFile.ReadLine()

    txtFile.Close
    fs.DeleteFile (txtFileName)
    Set data = ReadObject(line)

    Dim doc
    Dim docVar, varIndex

    Set doc = ActiveDocument
        
        '检查所有的客户化属性，赋初值
        Dim intProp, cusProp
        For intProp = 1 To doc.CustomDocumentProperties.Count
            If doc.CustomDocumentProperties(intProp).Value = "" Then
                doc.CustomDocumentProperties(intProp).Value = " "
            End If
        Next
        
        '更新属性值
        Dim t, dataName, dataVal
        For Each t In data
            dataName = t
            For Each cusProp In doc.CustomDocumentProperties
                'WScript.Echo dataName&" == "&cusProp.Name
                If cusProp.Name = dataName Then
                    '锡压变更，若已有值则不赋值
                    'If cusProp.Value = "" Or cusProp.Value = "*" Or cusProp.Value = " " Then
                    cusProp.Value = data(dataName)
                    'End if
                    'WScript.Echo "cusProp.Value = data(dataName)"
                    Exit For
                End If
            Next
        Next
        
        Dim aStory, aField
        For Each aStory In doc.StoryRanges
          For Each aField In aStory.Fields
            aField.Update
          Next
        Next
        
        Dim sh
        Dim l1
        Dim n1
        Dim n2
        Dim Name

        On Error Resume Next
        For Each sh In doc.Shapes
            startPos = -1: endPos = -1
            Name = Trim(sh.TextFrame.TextRange.Text)

            If Left(Name, 2) = "&[" And Mid(Name, Len(Name) - 1, 1) = "]" Then
                l1 = Len(Name)
                n1 = Right(Name, l1 - 2)
                n2 = Left(n1, l1 - 4)
                Name = n2

                If data.Exists(Name) Then
                    Value = data(Name)

                    sh.TextFrame.TextRange.Text = Value
                    wordApp.Selection.Find.Execute wdReplaceAll
                        'WScript.Echo "wordApp.Selection.Find.Execute wdReplaceAll"
                    Else
                        Value = "(?)"
                        'WScript.Echo "(?)"
                        'doc.Range(startPos-2, endPos+1).Text = value
                End If
                    ' WScript.Echo "Substituting " & doc.Range(startPos, endPos).Text & " to " & value
                    startPos = -1: endPos = -1
            End If
        Next
       End If
     
        End Sub
        
                
        Function ReadObject(dataline)
            Dim data, nvpair, nv
            Set ReadObject = CreateObject("Scripting.Dictionary")
            data = Split(dataline, "|")
            For Each nvpair In data
                'MsgBox nvpair
                nv = Split(nvpair, "=")
                If UBound(nv) >= 1 Then
                    If Not ReadObject.Exists(nv(0)) Then
                        ReadObject.Add nv(0), SubstEscSeq(nv(1))
                    End If
                End If
            Next
        End Function
        
        Function digitValue(hexStr)
            digitValue = InStr(digitDict, hexStr) - 1
            'WScript.Echo "Decode hex char " & hexStr & " to " & digitValue
        End Function
        
        Function SubstEscSeq(str)
            Dim startPos, tokenPos, strLen
            SubstEscSeq = ""
            strLen = Len(str)
            startPos = 1
            Do While startPos < strLen
                tokenPos = InStr(startPos, str, "%")
                If tokenPos < 1 Then
                    Exit Do
                End If
                SubstEscSeq = SubstEscSeq + Mid(str, startPos, tokenPos - startPos)
                'WScript.Echo "Hex token " & Mid(str, tokenPos, 3) & " decoded to asc value: " & digitValue(Mid(str, tokenPos+1, 1)) * 16 + digitValue(Mid(str, tokenPos+2, 1))
                SubstEscSeq = SubstEscSeq + Chr(digitValue(Mid(str, tokenPos + 1, 1)) * 16 + digitValue(Mid(str, tokenPos + 2, 1)))
                startPos = tokenPos + 3
            Loop
            SubstEscSeq = SubstEscSeq + Mid(str, startPos, strLen - startPos + 1)
            'WScript.Echo "Decoded string from " & str & " to " & SubstEscSeq
        End Function
    
    
    


