Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用vba将word文档的内容复制到另一个word文档中_Vba_Ms Word - Fatal编程技术网

使用vba将word文档的内容复制到另一个word文档中

使用vba将word文档的内容复制到另一个word文档中,vba,ms-word,Vba,Ms Word,我已经很多年没有使用VB了,所以如果这是显而易见的,请原谅我。我正在尝试编写一个word vba宏,用于显示用户表单的模板,然后根据用户表单导入fileA.docx、fileB.docx或fileC.docx的内容。(之后我将使用书签填写一些表单数据,我不知道这是否相关)。文件A、B和C将包含一些基本格式(如列表)的文本,但没有什么特别之处 我在网上看到的解决方案可以将文件内容复制到一个新文件中,但理想情况下,我希望将其中一个文件的全部导入到从模板中获取的当前未命名的新文件中。我想我遇到的问题是

我已经很多年没有使用VB了,所以如果这是显而易见的,请原谅我。我正在尝试编写一个word vba宏,用于显示用户表单的模板,然后根据用户表单导入fileA.docx、fileB.docx或fileC.docx的内容。(之后我将使用书签填写一些表单数据,我不知道这是否相关)。文件A、B和C将包含一些基本格式(如列表)的文本,但没有什么特别之处

我在网上看到的解决方案可以将文件内容复制到一个新文件中,但理想情况下,我希望将其中一个文件的全部导入到从模板中获取的当前未命名的新文件中。我想我遇到的问题是将选择切换到这些文件中的一个,然后返回到新的未命名文档,尽管我可以用手确保复制正确


更新:我把事情弄得太难了,尽管这里的答案让我指向了正确的方向(谢谢!)。最后我就这么做了

ThisDocument.Activate

Selection.InsertFile("fileA")
这给了我想要的所有东西的原始转储。

录制一个宏

  • 从源文档开始
  • 按ctrl-a选择所有内容
  • 按ctrl-c将其复制到剪贴板
  • 切换到目标文档
  • 按ctrl-v键粘贴到文档中
  • 停止录音
  • 或(假设word 2007或更高版本)

  • 从目标文档开始,关闭源文档
  • 在功能区上单击“插入>对象>文件中的文本…”
  • 导航到源文档
  • 单击“插入”按钮
  • 停止录音

  • 我更喜欢第二个版本,因此我应该将其放在第一位,使用以下命令可以在使用的文档和复制粘贴元素之间切换:

    ThisDocument.Activate 'Sets the main document active
    Documents("Name.doc").Activate 'Activates another document
    
    您可以使用复制命令在文档中插入、复制和粘贴内容

    ThisDocument.Range.InsertAfter("String") 'Insert text
    
    Selection.WholeStory 'Select whole document
    Selection.Expand wdParagraph 'Expands your selection to current paragraph
    Selection.Copy 'Copy your selection
    Documents("name.doc").Activate 'Activate the other document
    Selection.EndKey wdStory 'Move to end of document
    Selection.PasteAndFormat wdPasteDefault 'Pastes in the content
    

    然后,您可以对这些文档进行格式化,或者使用以前的原始格式复制并粘贴它们。

    我也在做同样的事情,试图选择另一个文档,复制并粘贴。但它不起作用(我收到一个错误,可能是因为其他应用程序正在使用剪贴板,但我不确定)。所以我做了一些搜索,在微软开发中心找到了完美的解决方案


    这里有一个重要的改进(我认为),您将希望合并,因为它:

  • 不使用剪贴板,因此不会使宏易受用户在宏运行时更改剪贴板内容的攻击
  • 不使用文件,因此通过消除I/O大大提高了速度,并消除了必须处理文件系统安全/权限等问题的可能性。请不要使用.InsertFile(),如果在文档中循环,则会降低速度。在最后使用一次——只有在你必须的时候。下面的示例演示了如何在不使用.InsertFile()的情况下实现相同的结果
  • 其思想是将在一个源文档中找到的部分文本传输到与源文档不同的目标文档,并保留源文档的格式

    要完成上述操作(跳过打开文档的代码):


    这可以集成到我的用户表单的vba脚本中吗?关键是我需要根据用户输入选择将哪个文件转储到当前文档中。我希望避免复制userform代码来将字段填充到fileA、fileB等中。记录为宏的任何内容最终都会成为VBA,因此可以粘贴到逻辑中并修改为使用变量i8n而不是硬连接的名称。在VBA IDE中打开Normal>Modules>NewMacros,您将看到它。如果您在开始录制宏时为其命名,则会有所帮助。感谢指向
    FormattedText
    属性的指针!(,)不确定这是否是一个问题,但不需要选择。仅使用2个范围对象及其.FormattedText属性就足够了。用户的选择不会被不必要地更改。如果我可以补充的话,下面来自Andras Kovacs的帖子正在传播关于如何使用.InsertFile()函数的误导性信息。请阅读我的文章全文,找出为什么要尽量减少使用该功能。
    Selection.Collapse Direction:=wdCollapseEnd 
    Selection.InsertFile FileName:="C:\TEST.DOC"
    
    For Each oTable In oDoc_Source  
    'the above could have been anything that returns a Range object
    'such as: ActiveDocument.Content.Find.Execute ....
    
    '...
    'logic here to identify the table, or text, you are looking for
    '...
    
    'I can't believe the MS Dev Center folks could only think
    'of .InsertFile(), which is the last resort I would go for, 
    'especially if your code runs on a web server [concurrent web requests]!
    
    'SAFEST
    '(no user interference on clipboard possible, no need to deal with file i/o and permissions)
    'you need a reference to Document.Content, 
    'as the act of obtaining a reference "un-collapses" the range, so the below 3 lines must be in that order.
    Set oRange =  oDoc_DestinationDoc.Content
    oRange.Collapse Direction:=wdCollapseEnd
    oRange.FormattedText = oTable.Range
    
    'BRUTE, AND PRONE TO RANDOM ERRORS AND HANGS DUE TO USER INTERFERENCE WITH CLIPBOARD 
    'find a way to implement WIHTOUT using the CLIPBOARD altogether to copy the below range object
    'it will be easier for PC users to use the clipboard while the macro runs
    'and it will probably be safer for the output of this macro to remain uncorrupted
    
    'oTable.Range.Copy
    'Set oRange =  oDoc_DestinationDoc.Content
    'oRange.Collapse Direction:=wdCollapseEnd
    'oRange.Paste
    
    
    'THE BELOW DOES NOT WORK
    ' '1) - cannot add a range from another document
    ' 'adds only text, not the formats and not the table layout
    ' oTable.Range.TextRetrievalMode.IncludeFieldCodes = True
    ' oTable.Range.TextRetrievalMode.IncludeHiddenText = True
    '  oDoc_DestinationDoc.Content.InsertAfter oTable.Range
    '
    ' '2) - cannot add a range from another document
    '  oDoc_DestinationDoc.Content.Tables.Add oTable.Range, iRowMax, iColMax
    ' 
    ' '3) - only puts in plain text, and it replaces the range without the .Collapse call
    '  oDoc_DestinationDoc.Content.Text = oTable.Range
    
    'set current doc name and path
        Dim docName As String: docName = ActiveDocument.name
        Dim filepath As String: filepath = ActiveDocument.Path
    'create a new file
        Documents.Add
    'get the path of a current file
        ChangeFileOpenDirectory filepath
    'insert content of current file to newly created doc
        Selection.InsertFile _
            FileName:=docName, _
            Range:="", _
            ConfirmConversions:=False, _
            Link:=False, _
            Attachment:=False
    'open prompt to save a new file
        With Dialogs(wdDialogFileSaveAs)
            .name = docName & "-copy"
            .Show
        End With