Vba 更新word文档中的所有excel嵌入链接

Vba 更新word文档中的所有excel嵌入链接,vba,excel,ms-word,updating,Vba,Excel,Ms Word,Updating,谢谢nick,我试过了,但是我必须指定excel应用程序,因为它是在word VBA中工作的。我还减少了代码,这样它只会更改链接和Odest,但仍然很慢。我不能理解这一点,因为当我只更新链接使用 Activedocument.fields.update 它马上就完成了。我知道这只是更新,而不是改变来源,但仍然是时间差太大 我的全部代码是: Private Sub CommandButton1_Click() Dim OldFile As String Dim xlsobj As Object

谢谢nick,我试过了,但是我必须指定excel应用程序,因为它是在word VBA中工作的。我还减少了代码,这样它只会更改链接和Odest,但仍然很慢。我不能理解这一点,因为当我只更新链接使用

Activedocument.fields.update
它马上就完成了。我知道这只是更新,而不是改变来源,但仍然是时间差太大

我的全部代码是:

Private Sub CommandButton1_Click()

Dim OldFile As String
Dim xlsobj As Object
Dim xlsfile_chart As Object
Dim dlgSelectFile As FileDialog 'FileDialog object '
Dim thisField As Field
Dim selectedFile As Variant
'must be Variant to contain filepath of selected item
Dim newFile As Variant
Dim fieldCount As Integer '
Dim x As Long
On Error GoTo LinkError
'create FileDialog object as File Picker dialog box
Set dlgSelectFile = Application.FileDialog
(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
.Filters.Clear 'clear filters
.Filters.Add "Microsoft Excel Files", "*.xls, *.xlsb, *.xlsm,  
*.xlsx" 'filter for only Excel files
'use Show method to display File Picker dialog box and return user's 
action
If .Show = -1 Then
 'step through each string in the FileDialogSelectedItems collection
 For Each selectedFile In .SelectedItems
   newFile = selectedFile 'gets new filepath
 Next selectedFile
 Else 'user clicked cancel
 Exit Sub
 End If
 End With
 Set dlgSelectFile = Nothing
'update fields



 Set xlsobj = CreateObject("Excel.Application")
 xlsobj.Application.Visible = False
 Set xlsfile_chart = xlsobj.Application.Workbooks.Open(newFile,  
 ReadOnly   = True)

 Application.ScreenUpdating = False

  With xlsobj.Application
 .calculation = xlcalculationmanual
 .enableevents = False
 End With



  fieldCount = ActiveDocument.Fields.Count
   For x = 1 To fieldCount
   With ActiveDocument.Fields(x)
   If .Type = 56 Then
  .LinkFormat.SourceFullName = newFile
    End If
   End With
  Next x

  With xlsobj.Application
 .calculation = xlcalculationmanual
 .enableevents = True
 End With

 Application.ScreenUpdating = True



  MsgBox "Data has been sucessfully linked to report"

   'clean up
   xlsfile_chart.Close SaveChanges:=False
   Set xlsfile_chart = Nothing
   xlsobj.Quit
   Set xlsobj = Nothing


    Exit Sub
  LinkError:
  Select Case Err.Number
   Case 5391 'could not find associated Range Name
    MsgBox "Could not find the associated Excel Range Name " & _
   "for one or more links in this document. " & _
   "Please be sure that you have selected a valid " & _
   "Quote Submission input file.", vbCritical
    Case Else
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
   End Select


   ' clean up
    Set xlsfile_chart = Nothing
     xlsobj.Quit
     Set xlsobj = Nothing

    End Sub
添加以下内容:

Application.ScreenUpdating=False

With Application
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With

        'Your code here.

With Application
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With

Application.ScreenUpdating=True
添加以下内容:

Application.ScreenUpdating=False

With Application
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With

        'Your code here.

With Application
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With

Application.ScreenUpdating=True

代码中的新文件只是一个从对话框中获取的文件路径,用户在该对话框中选择链接excel文件,因此我认为这部分很好。我认为这里的循环可能是做了不必要的更新或其他什么?代码中的新文件只是一个从用户选择链接excel文件的对话框中获取的文件路径,所以我认为这部分很好。我想这里的循环可能是做了不必要的更新或什么的?谢谢nick,我试过了,修改了我的代码,但仍然很慢。这是vba这个词,所以我不知道这是不是一个要点。我在上面添加了我的完整代码。如果你能给我更多的建议,我将不胜感激。那么也许能找到一种方法来取代这些?我认为,有一种方法可以在代码中使用布尔语句进行变通,如果可以的话,idk会更快,但也许会更快?看看这个:谢谢尼克,我试过了,修改了我的代码,但是仍然很慢。这是vba这个词,所以我不知道这是不是一个要点。我在上面添加了我的完整代码。如果你能给我更多的建议,我将不胜感激。那么也许能找到一种方法来取代这些?我认为,有一种方法可以在代码中使用布尔语句进行变通,如果可以的话,idk会更快,但也许会更快?看看这个: