使用Word VBA更新Excel链接的图表源

问题描述 投票:0回答:1

我正在尝试在MS Word中设置一个按钮,以更新图表后面的链接的Excel数据源。我先要说一下,我知道使用内置功能很容易,但是我想为那些不方便的菜单导航使用一个按钮。另外,我通常阅读并从一些有关该主题的精彩文章中汲取灵感,但是遇到了那些文章中未提及的错误。

测试表明,Word a)将Excel链接标识为字段(相对于形状),b)将该字段标记为类型87(OCX)。当我运行下面的代码时,它将生成“对象变量或未设置块变量”错误。但是,此[[appears可能根植于基于此Microsoft article的字段类型。

我正在努力寻找一种替代方法,希望能获得帮助...这是我首次尝试VBA for Word。谢谢!

Sub CommandButton1_Click() Dim MyNewFile As Variant Dim fDialog As FileDialog, result As Integer Dim fieldCount As Integer 'Set up file dialog Set fDialog = Application.FileDialog(msoFileDialogFilePicker) With fDialog .AllowMultiSelect = False .Title = "Select a file" .Filters.Clear .Filters.Add "Excel files", "*.xlsx,*.xlsb,*.xlsm,*.xls" End With 'Show the dialog, store the file name If fDialog.Show = -1 Then MyNewFile = fDialog.SelectedItems(1) End If fieldCount = ActiveDocument.Fields.Count For k = 1 To fieldCount With ActiveDocument.Fields(k) If .Type = 87 Then With .LinkFormat .SourceFullName = MyNewFile 'BLOWS UP HERE .AutoUpdate = True End With End If End With Next k End Sub

vba ms-word
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.