繁体   English   中英

Excel VBA宏-用Excel中255以上的句子替换Microsoft Word上的参考词

[英]Excel VBA Macro - Replace a Reference word on Microsoft Word with a sentence above 255 from Excel

我想用大于Excel中255个单词的段落替换Word上的参考单词[Property Manager Notes]。 将会有更多类似的参考文献。

任何人都可以帮忙。

这是获得想法的图片:

[图片

这是我正在使用的代码

 Dim objWord
   Dim objDoc
   Dim oCell  As Integer
  Sub Replacing_excel_word()

 Sheets("Work").Select

   Set objWord = CreateObject("Word.Application")
   Set objDoc = objWord.Documents.Open("C:\Users\Sai\Desktop\xyz.docx")

objWord.Visible = True

objWord.Activate

For oCell = 1 To 50

from_text = Sheets("Work").Range("A" & oCell).Value
to_text = Sheets("Work").Range("B" & oCell).Value

With objWord.ActiveDocument
    Set myRange = .Content
    With myRange.Find
        .Execute FindText:=from_text, ReplaceWith:=to_text, Replace:=1
    End With
End With

Next oCell

End Sub

我刚刚解决了这个问题。 在代码的最后,您需要创建一个函数:

Function Replacement255(wRng, field, content)


        With wRng.Find
            .text = field   '>> enter the text to be searched and replaced
            .MatchWholeWord = False
            .MatchWildcards = False

          If Len(content) > 255 Then
            .Wrap = wdFindContinue
              cnt = 0
              Do
                 strFragment1 = Mid(content, cnt + 1, 230)
                 cnt = cnt + 230
                 If Len(strFragment1) > 0 Then strFragment1 = strFragment1 & "@@@@@@@@@@"
                 .Replacement.text = strFragment1
                 .Execute , , , , , , , , , , wdReplaceOne
                 .text = "@@@@@@@@@@"
              Loop While Len(strFragment1) > 0
          Else
            .Replacement.text = text  '>> enter column location of the text from excel
                .Wrap = wdFindStop
              .Execute Replace:=wdReplaceOne
          End If

          End With

End Function

然后,在您的代码中,您需要使用以下功能:

 Replacement255 wRng, field, content.Value

哪里:

栏位:取代ex的文字。 “ #Enter here#”内容:用ex替换的文本。 Sheet1.Cells(MyRow, "M")

希望对您有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM