用于插入文件和合并格式的 Word VBA 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16502534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:03:53  来源:igfitidea点击:

Word VBA macro to insert file and merge formatting

vbams-wordword-vba

提问by Richard Herron

I have a template with a header/footer and text formatting. I would like to write a macro to fill this template with the contents of an .rtf or .doc file. Also, I would like to merge the formatting so that I keep the header and formatting from the template file, and the pictures in the .rtf or .doc files.

我有一个带有页眉/页脚和文本格式的模板。我想编写一个宏来用 .rtf 或 .doc 文件的内容填充此模板。此外,我想合并格式,以便保留模板文件中的标题和格式,以及 .rtf 或 .doc 文件中的图片。

Cut-and-paste works great. If I open and save the template file, open the file to insert, select all, and paste special with "merge formatting", then I get exactly what I want. I just want a more scalable solution.

剪切和粘贴效果很好。如果我打开并保存模板文件,打开要插入的文件,选择全部,然后使用“合并格式”进行特殊粘贴,那么我得到的正是我想要的。我只想要一个更具可扩展性的解决方案。

I wrote a macro that does most of this, but it fails to merge the formatting and drops (or hides) the header and footer. I thought the correct approach would use the InsertFilemethod, but I can't figure it out.

我写了一个宏来完成大部分工作,但它无法合并格式并删除(或隐藏)页眉和页脚。我认为正确的方法是使用InsertFile方法,但我想不通。

Any pointers would be appreciated (I'm new to both Word and VBA).

任何指针将不胜感激(我是 Word 和 VBA 的新手)。

Sub InsertFile()

    currentPath = ActiveDocument.Path

    Set FileBox = Application.FileDialog(msoFileDialogFilePicker)

    With FileBox
        .Title = "Select the File that you want to insert"
        .InitialFileName = currentPath & "\" & "*.rtf"
        .AllowMultiSelect = False
        If .Show = -1 Then
            FiletoInsert = .SelectedItems(1)
        End If
    End With

    Selection.Range.InsertFile FiletoInsert
    Set FileBox = Nothing
End Sub


Update - I also tried this approach, which seems to use cut-and-paste, but the results are the same.

更新 - 我也尝试过这种方法,它似乎使用了剪切和粘贴,但结果是一样的。

回答by Richard Herron

Here's the best that I can do. It pastes as plain text, but that's better than nothing (or pasting with original formatting).

这是我能做的最好的事情。它粘贴为纯文本,但这总比没有好(或使用原始格式粘贴)。

Sub InsertFile()
    ' inserts selected file into current document (strips formatting)

    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Title = "Select the File that you want to insert"
        .Show
        FiletoInsert = .SelectedItems(1)
    End With

    ' get content from my file
    Application.Documents.Open (FiletoInsert)
    Application.Selection.WholeStory
    Application.Selection.Copy
    Application.ActiveWindow.Close

    ' paste without formatting
    Application.Selection.PasteSpecial DataType:=wdPasteText

End Sub

回答by Malp

Sub InsertFile()

    ' inserts selected file into current document (strips formatting)

    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Title = "Select the File that you want to insert"
        .Show
        FiletoInsert = .SelectedItems(1)
    End With
    Selection.InsertFile FileName:=FiletoInsert, Range:="", _
        ConfirmConversions:=False, Link:=False, Attachment:=False
End Sub

回答by user3610011

I've tried this same call in my own VBA macro, and find that

我在自己的 VBA 宏中尝试过同样的调用,发现

Selection.Range.InsertFile (FiletoInsert)

Selection.Range.InsertFile (FiletoInsert)

Seems to work when I only pass the one parameter filename. Make sure the filename is complete.

当我只传递一个参数文件名时似乎有效。确保文件名完整。