【问题标题】:VBA to find specific text in word doc and copy this text from word doc into a cell in excelVBA在word doc中查找特定文本并将此文本从word doc复制到excel中的单元格中
【发布时间】:2020-01-28 01:52:58
【问题描述】:

你好 stackoverflow 社区。​​p>

到目前为止,我正在做的是从我之前打开的 word 文档中手动复​​制价格,然后将其粘贴到 Excel 工作表中。 它是当时在电脑上打开的唯一.docx 文件,所以我们只需要在当前打开的word 文件中查找价格即可。 我希望你帮我自动完成这项任务。

这张图片显示了我复制价格的文件部分。 在本例中,它是 605.000。但是在word文件中查看之前我不知道价格。 word文件是我了解价格的地方。 所选文本在整个文档中仅出现一次,因此我需要 VBA 复制“brutto w kwocie”之后的内容,直到第一个昏迷。 是的 - 只有没有小数的金额,因为它们总是 ,00。 但不仅仅是七个标志,因为如果我的公寓价格为 1.250.000,那么只复制 7 个标志的宏将不起作用。

Sub Find_Price()
    'Variables declaration
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim TextToFind As String
    Dim ApartmentPrice As String
    Dim Rng As Word.Range
    Application.ScreenUpdating = False
    'This is the text I'm looking for in .ActiveDocument
    TextToFind = "brutto w kwocie "
    'Start Word and create an object
    'Set WordApp = CreateObject("Word.Application")
    'Reference already opened Word document from excel VBA console
    Set WordApp = GetObject(, "Word.Application")
    WordApp.Application.Visible = True
    Set Rng = WordApp.ActiveDocument.Content
    'Set WordDoc = WordApp.ActiveDocument   'I don't know how to finish this line

        Rng.Find.Execute FindText:=TextToFind, Forward:=True     
                'what this "Forward:=True" means??
        If Rng.Find.Found Then
            If Rng.Information(wdWithInTable) Then
              'I don't know how to write this part of the code.
              'Please don't remove my question again - I've researched 16h for this info.
              MsgBox "Price is " & ApartmentPrice & " pln."
            End If
        Else
            MsgBox "Apartment price was not found!"
        End If
    Set ws = ActiveSheet       'currently opened sheet on currently opened.xlsm file
    ws.Range("E27").Activate
    ws.Paste
End Sub

然后我需要从金额中间这个可笑的点中去除数字,所以请帮助我将 605.000 清除为 60500 或将 1.250.000 清除为 1250000。

当我在剪贴板中有这个数字(价格)时,我需要将它粘贴到当前打开的 excel 文件中,粘贴到 .activesheet 中(因为 excel 文件和 excel 表的名称每天会更改很多次)。 但目标单元格将始终是 E27 - 它永远不会改变。

感谢大家的帮助。


编辑 24.01.2020 这是我尽我所能修改的上述代码。

Sub Find_Corrected()
    'Variables declaration
    'Dim WordApp As Object
    Dim WordApp As Word.Application
    'Dim WordDoc As Object
    Dim WordDoc As Word.Document
    Dim TextToFind As String
    Dim ApartmentPrice As String
    Dim Rng As Word.Range
    Application.ScreenUpdating = False
        'This is the text I'm looking for in .ActiveDocument
        TextToFind = "brutto w kwocie "
        'Start Word and create an object
        'Set WordApp = CreateObject("Word.Application")
        'Reference already opened Word document from excel VBA console
        Set WordApp = GetObject(, "Word.Application")
        Set WordDoc = WordApp.ActiveDocument
        Set Rng = WordApp.ActiveDocument.Content
        WordApp.Application.Visible = True
        'Set WordDoc = WordApp.Documents.Open(FilePath & "Form1.docx")
        'Set WordDoc = WordApp.ActiveDocument     'I don't know how to finish this line  :-(
            Rng.Find.Execute FindText:=TextToFind, Forward:=True
                    'what this "Forward:=True" means??
            With Rng.Find
                .Text = "brutto w kwocie "
                .Execute
                    If .Found = True Then
                        Rng.MoveEnd wdWord, 3
                        Rng.Copy
                        MsgBox "Copied value equals " & Rng.Value & " Roesler conquers."
                    Else
                        MsgBox "Requested range was not found!"
                    End If
            End With
    'Set ws = ActiveSheet       ' currently opened sheet on currently opened.xlsm file
    'ws.Range("E27").Activate
    'ws.Paste
End Sub

这是它返回的错误。

【问题讨论】:

  • 进入 Word 的高级查找(例如,按 Ctrl+H)。激活搜索通配符的选项(您需要单击“更多”才能看到),然后使用此搜索词进行测试:za cene brutto w kwocie [0-9.]{1;100} 如果可行,那么您的宏可以删除 za cene brutto w kwocie 并且您已经获得了号码。
  • 有没有办法通过我正在编写的这个宏中的 vba 代码打开“搜索通配符”选项?因为当我在Ctrl+H菜单中手动开启时,一直没有按钮接受它,而当我再次打开查找和替换菜单时,“搜索通配符”选项仍然未选中。
  • 你好@Cindy Meister。你能解释一下这个dot是如何改变搜索词za cene brutto w kwocie [0-9.]{1;100}我在互联网上找不到的。我只是从通配符开始我的冒险。我找到了[0-9][!0-9] 的解释,但我找不到[0-9.]
  • 圆点在您的示例文本中,作为数字的一部分。所以术语[0-9.] 搜索所有这些字符:数字0 到9 和一个点。如果您将a! 放在方括号中,这些也是“查找”的有效字符。
  • 你的其他评论我不太明白。如果您问是否可以更改表单的默认设置,那么,不,不是。

标签: excel vba ms-word copy paste


【解决方案1】:

您可以使用我在an answer to another of your questions.中使用的相同方法

创建一个范围,将其设置为等于整个文档,沿范围搜索,移动到所需的停止范围,然后将范围的起点移动到您的数字。

Dim srchRng as Range
Set srchRng = ActiveDocument.Content

With srchRng.Find
    .Text = "brutto w kwocie "
    .Execute
    If .Found = True Then
        Dim numberStart as Long
        numberStart = Len(srchRng.Text) + 1
        srchRng.MoveEndUntil Cset:=","

        Dim myNum as String
        myNum = Mid(srchRng.Text, numberStart)
    End If
End With

【讨论】:

    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2014-01-27
    • 1970-01-01
    相关资源
    最近更新 更多