在Excel中查找并替换嵌入的单词doc后,更改将保存在嵌入的单词doc中

时间:2017-12-14 15:39:05

标签: excel vba replace ms-word

我正在构建一个使用模板的代码(嵌入在Excel中的Word文档),并使用Excel的输入查找和替换模板中的某些单词。我已成功编码模板的开头,在模板中查找和替换。

但之后,当我在Excel中检查嵌入的Word文档时,已保存替换的单词。我不想覆盖模板的内容,但每次运行代码时,它都会自动保存查找和替换期间所做的更改。我只是想让它找到并替换,然后将副本保存到我的本地文件夹。

我使用后期绑定,因为我们的团队正在使用的Excel版本存在限制。

我不知道以下代码的功能是否导致更改保存在嵌入式Word文档中。

.Execute Replace:=2 'wdReplaceAll

这是我的完整代码:

Sub Button1_Click()

Application.ScreenUpdating = False

Set WDApp = CreateObject("Word.Application")
WDApp.Visible = True

Set WDDoc = Sheets("Sheet1").OLEObjects("Template_112225")
WDDoc.Verb Verb:=xlOpen

WDApp.Selection.WholeStory

Call SplitCell

Call Find("<Part Num>", Sheets("Sheet2").Cells(8, 4).Value)
Call Find("<Dataset>", Sheets("Sheet2").Cells(7, 3).Value)
Call Find("<Letter>", Sheets("Sheet2").Cells(8, 5).Value)

Set WDDoc = Nothing
Set WDApp = Nothing
Set Rng = Nothing

End Sub

Sub Find(Find_Value As String, New_Value As String)
With WDApp.Selection.Find
    .Text = Find_Value
    .Replacement.Text = New_Value
    .Forward = True
    .Wrap = 1 'wdFindContinue
    .Execute Replace:=2 'wdReplaceAll
End With
End Sub

Sub SplitCell()
Dim txt As String
Dim i As Integer
Dim NumberLetter As Variant

txt = Sheets("Sheet2").Cells(8, 3).Value

NumberLetter = Split(txt, "/")

For i = 0 To UBound(NumberLetter)
    Cells(8, i + 4).Value = NumberLetter(i)
Next i

End Sub    

是否可以使用一个代码来显示“另存为”对话框?因此,用户可以选择保存修改后的副本的位置。

0 个答案:

没有答案
相关问题