Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 用于重新链接Word文档的VB宏问题_Vba_Excel - Fatal编程技术网

Vba 用于重新链接Word文档的VB宏问题

Vba 用于重新链接Word文档的VB宏问题,vba,excel,Vba,Excel,我无法使此宏正常工作。我正在使用Office 2013操作Windows 10计算机。代码不是我写的,我在VB方面的知识有限。marco应该通过镜像上一个word文档的链接方式来更新word文档的链接。下面是代码,如果有人能帮上忙,那就太好了 Sub relinking() Dim oriacro As String Dim taracro As String Dim path As String oriacro = InputBox(Prompt:="please enter the or

我无法使此宏正常工作。我正在使用Office 2013操作Windows 10计算机。代码不是我写的,我在VB方面的知识有限。marco应该通过镜像上一个word文档的链接方式来更新word文档的链接。下面是代码,如果有人能帮上忙,那就太好了

Sub relinking()

Dim oriacro As String
Dim taracro As String
Dim path As String

oriacro = InputBox(Prompt:="please enter the original agency acronym.", Title:="ENTER THE ORIGINAL AGENCY ACRONYM")
taracro = InputBox(Prompt:="please enter the target agency acronym.", Title:="ENTER THE TARGET AGENCY ACRONYM")
path = InputBox(Prompt:="please enter the target path.", Title:="ENTER THE TARGET PATH")

Excel.Application.Quit
'close all the excel files.(excel reference has to be activated in tool->reference'

For x = 1 To ActiveDocument.Fields.Count
'the program runs over all the linked fields'

If Left(ActiveDocument.Fields(x).LinkFormat.SourceNam e, Len(oriacro)) = oriacro Then
'read all the fields that has "original agency acronym" in the beginning of its linked excel files.'
ActiveDocument.Fields(x).LinkFormat.SourceFullName = path & "\" & taracro & "_" & Right(ActiveDocument.Fields(x).LinkFormat.SourceNa me, Len(ActiveDocument.Fields(x).LinkFormat.SourceName ) - InStr(ActiveDocument.Fields(x).LinkFormat.SourceNa me, "_"))
'Assign the fields with new links that are created from combining the "target path" ,"target agency acronym", and the parts of the names right after the original acronyms of the original linked file names.'
Else
'Leave other linked fields as they are.'
End If
Next x

MsgBox ("All Fields have been relinked!")
End Sub 
1) 上面代码中的一些打字错误可能只是在StackOverflow上格式化:
.sourcename
几次。在生成新路径的函数中:

Right(ActiveDocument.Fields(x).LinkFormat.SourceNa me, Len(ActiveDocument.Fields(x).LinkFormat.SourceName ) - InStr(ActiveDocument.Fields(x).LinkFormat.SourceNa me, "_")). 
还要添加一个字符串变量,并在msgbox中捕获其中一个变量以进行测试:

If Left(ActiveDocument.Fields(x).LinkFormat.SourceNam e, Len(oriacro)) = oriacro Then

    'read all the fields that has "original agency acronym" in the beginning of its linked excel files.'
    str = ActiveDocument.Fields(x).LinkFormat.SourceName 'Dim str variable above if necessary
    ActiveDocument.Fields(x).LinkFormat.SourceFullName = path & "\" & taracro & "_" & Right(str, InStrRev(str, "_")-1)
    'Assign the fields with new links that are created from combining the "target path" ,"target agency acronym", and the parts of the names right after the original acronyms of the original linked file names.'
    msgbox ActiveDocument.Fields(x).LinkFormat.SourceFullName   'does this message show the proper path??
Else
    'Leave other linked fields as they are.'
End If
ActiveDocument.Fields.Update

marco
?你是说宏?