尝试从 excel 打开 word 文档,执行查找和替换,然后复制结果

尝试从 excel 打开 word 文档,执行查找和替换,然后复制结果

但是这个宏没有执行查找和替换。

它可以做其他所有事情...但就是做不到这一点。

Sub ImportSectHWord()
'It is better to always define constants, even though they will default to zero
' which just happens to be the desired value in this case
Const wdDoNotSaveChanges As Long = 0

Dim objWord As Object
    Set objWord = CreateObject("word.Application")
        objWord.Visible = True
Dim objDoc As Object
Dim wdFileName
wdFileName = ActiveWorkbook.Path & "\" & ActiveSheet.Name & ".docm"
Dim strSheetName As String
strSheetName = ActiveSheet.Name

objWord.Documents.Open (wdFileName) ', ReadOnly:=True

Set objDoc = objWord.Documents.Open(wdFileName)

'objDoc.Documents.Open (wdFileName)

        With objDoc.Content.Find
            .text = "^p"
            .Replacement.text = "^v"
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll

        End With
        Set wdApp = Nothing: Set wdDoc = Nothing


objWord.Selection.WholeStory
objWord.Selection.Copy
ActiveWorkbook.Worksheets(strSheetName).Select
ActiveWorkbook.Worksheets(strSheetName).Range("A1").Select
ActiveWorkbook.ActiveSheet.Paste



ActiveCell.Copy     'clears the clipboard before closing word.
'objWord.Close                 'clears the clipboard before closing word.

objDoc.Close 'SaveChanges:=wdDoNotSaveChanges
objWord.Quit

WordTable修复程序

子目录结束

我认为我的问题是这样的:

With objDoc.Content.Find
            .text = "^p"
            .Replacement.text = "^v"
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll

End With

抱歉,这个代码太乱了。我整个早上都在捣鼓它。

这里的目标是打开目标 word 文件(与 excel 中的工作表同名),执行两次查找/替换以清除所有错误的换行符,然后将其粘贴到 excel 中。在 excel 中执行第三次查找/替换(在 excel 中)以重新建立段落。

如果我只是按原样粘贴文本,我会得到各种我不想要的混乱的单元格合并和垃圾。

有什么想法吗?

相关内容