Word宏使用变量激活doc [英] Word Macro to make doc active using variable

查看:134
本文介绍了Word宏使用变量激活doc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个宏(将一个文档打开)打开现有文档,复制文档内容,然后切换回原始文档并粘贴内容。

I am trying to write a macro that will (with one document already open) open an existing document, copy the contents of the document, then switch back to the original document and paste the contents.

已经搜索过高低,试图将各种论坛中建议的代码拼凑起来,试图获得一个有效但尚未管理的解决方案。看起来这应该是一件相当简单的事情!

Have searched high and low trying to piece together bits of code suggested in various forums trying to get a solution that works but haven't managed it. It seems this should be a fairly simple thing to do!

下面的命令会使标题为thisdoc.doc的文档处于活动状态,这就是我的意思需要,但我需要切换"thisdoc.doc"。使用宏中较早设置的变量,例如"strFileName"

The command below will make the doc titled thisdoc.doc active which is what I need, but I need to switch "thisdoc.doc" with a variable set earlier in the macro, eg "strFileName"

Windows(" thisdoc.doc")。激活

Windows("thisdoc.doc").Activate

作为参考,整个宏如下所示:

For reference, the whole macro goes like this:

推荐答案

或许这样?

    '根据文档中的前两个单词设置文档名称。保存

    Dim docCurrent As Document

    Dim docOther As Document

    Dim rngFilename As Range

    Dim strFileName As String

   设置docCurrent = ActiveDocument

   设置rngFilename = docCurrent.Range

   使用rngFilename为
        .End = .Start

        .MoveEnd wdWord,2

        strFileName = .Text

   结束与$
    docCurrent.SaveAs FileName:=" D:\ Marketing& Website \Quote Template Documents \" &安培; _

        strFileName& " .doc"

    ' Set document name based on first two words in document & save
    Dim docCurrent As Document
    Dim docOther As Document
    Dim rngFilename As Range
    Dim strFileName As String
    Set docCurrent = ActiveDocument
    Set rngFilename = docCurrent.Range
    With rngFilename
        .End = .Start
        .MoveEnd wdWord, 2
        strFileName = .Text
    End With
    docCurrent.SaveAs FileName:="D:\Marketing & Website\Quote Template Documents\" & _
        strFileName & ".doc"

    '打开现有文档复制内容

   设置docOther = Documents.Open(FileName:=" D:\ pathname \otherdoc.doc",_

        AddToRecentFiles:=错误)

    docOther.Range.Copy

    ' Open existing doc to copy contents
    Set docOther = Documents.Open(FileName:="D:\pathname\otherdoc.doc", _
        AddToRecentFiles:=False)
    docOther.Range.Copy

    '切换回原始doc&粘贴内容

    docCurrent.Activate

    Selection.PasteAndFormat wdUseDestinationStylesRecovery

    ' Switch back to original doc & paste content
    docCurrent.Activate
    Selection.PasteAndFormat wdUseDestinationStylesRecovery


这篇关于Word宏使用变量激活doc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆