将.docx中的字符串替换为Excel中的值,保留Excel大小写

时间:2018-06-18 13:27:34

标签: vba excel-vba word-vba excel

我正在编写一个使用.docx模板的宏(在excel中),并用excel文件中的值替换字符串。一切正常,除了粘贴值的格式与替换字符串的格式相同(在这种情况下,所有粘贴的值都是大写),而不是excel的值格式。我怎么能改变这个代码,以便粘贴的值保持其代码?来自excel的格式?

Sub fromexcel()

Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets(1)
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("\\xxxxx\Template.docx")
objWord.Visible = True
objDoc.Activate

With objDoc.Range.Find


If ws.Range("B13").Value <> "" Then

objDoc.FormFields(3).CheckBox.Value = True

      .Text = "AAAAA"
      .ClearFormatting
      .Replacement.Text = ws.Range("B13").Value
      .Replacement.ClearFormatting
      .Replacement.Font.Italic = False
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute Replace:=wdReplaceAll
Else:
      .Text = "AAAAA"
      .ClearFormatting
      .Replacement.Text = ""
      .Replacement.ClearFormatting
      .Replacement.Font.Italic = False
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute Replace:=wdReplaceAll
End If

我尝试了不同的组合,有或没有&#34; .ClearFormatting&#34;和.format = true / false但似乎没什么用。

0 个答案:

没有答案