【问题标题】:Word 2010, Move cursor to specific text, replace the text with file contentWord 2010,将光标移动到特定文本,将文本替换为文件内容
【发布时间】:2013-10-21 04:19:39
【问题描述】:

我有一行文字写着“[EmbeddedReport]report goes here[/EmbeddedReport]”。

我想用空字符串替换“报告在此处”并将光标放在 [EmbeddedReport] 标记之后。然后我将运行以下代码...

With Selection.InsertFile ('c:\Temp\Report.rtf') 

这应该将报告的文本放在标记之间。我尝试使用以下代码定位光标。它似乎不起作用。

.Selection.Find 
.ClearFormatting 
.MatchWholeWord = False 
.MatchCase = False 
.Execute FindText:="report goes here" 

唯一的问题是光标不在 [EmbeddedReport] 和 [/EmbeddedReport] 之间,并且在运行宏之前将文件插入到光标所在的任何位置。

【问题讨论】:

    标签: vba text ms-word find cursor


    【解决方案1】:

    您可能需要拨打Find object 两次:

    FIRST- 找到整个短语[EmbeddedReport]report goes here[/EmbeddedReport]SECOND- 在第一步的结果中找到report goes here 文本。重要 - 您无需替换该短语 - 它将被选中并替换为您使用 Selection.InsertFile method 导入的文本。

    这是建议的代码(已针对示例文件进行测试):

     'FIRST- find [EmbeddedReport]report goes here[/EmbeddedReport]
        With Selection.Find
            .Text = "(\[EmbeddedReport\])text goes here(\[\/EmbeddedReport\])"
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWildcards = True
        End With
        '...and select it
        Selection.Find.Execute
    
        'SECOND- find only text to replace 'text goes here'
        With Selection.Find
            .Text = "text goes here"
            .Replacement.Text = " "
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWildcards = True
        End With
        'end select it
        Selection.Find.Execute
    
        'now you could insert your file
        Selection.InsertFile "c:\Temp\Report.rtf"
    

    【讨论】:

    • KazJaw 这确实摆脱了“报告在此处”,但文件仍然插入宏启动时光标所在的位置。光标似乎没有在 [EmbeddedReport] 和 [/EmbeddedReport] 标记之间移动。
    • KazJaw...对不起。这是我的错,它不起作用。我没有删除您的代码上方的一些代码。我仍然需要做一些测试,但我认为你已经回答了我的问题。有机会进一步测试后,我会将其标记为答案。我不必做第一个只查找第二个。再次感谢。
    • 刚才我在上面看到了你的第一条评论。如果您需要任何其他帮助,请在您的问题中添加完整/更多代码。
    【解决方案2】:

    解决此类问题的常用方法是录制宏 - 并执行移动光标和突出显示文本等所需的任务 - 然后检查宏为执行这些任务而编写的代码。这通常会让您了解如何定制自己的代码来执行相同的任务。

    【讨论】:

    • dav1dsm1th...老实说,在我发布问题后我确实想到了这一点,但感谢您的洞察力。感谢您抽出时间回复。
    • dav1dsm1th...对不起。这是我的错,它不起作用。我没有删除您的代码上方的一些代码。我仍然需要做一些测试,但我认为你已经回答了我的问题。有机会进一步测试后,我会将其标记为答案。再次感谢。
    • 我只提出了一种方法......没有代码。如果是 @KazJaw 回答了您的问题,请相信他提供了解决方案。
    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多