Word VBA宏以插入文件和合并格式
我有一个带有页眉/页脚和文本格式的模板。我想写一个宏,用.rtf或.doc文件的内容填充这个模板。此外,我希望合并格式,以便保留模板文件的标题和格式,以及.rtf或.doc文件中的图片 剪切粘贴效果很好。如果我打开并保存模板文件,打开要插入的文件,选择“全部”,然后使用“合并格式”粘贴“特殊”,那么我得到的正是我想要的。我只是想要一个更具可扩展性的解决方案 我编写了一个宏来完成大部分工作,但它无法合并格式并删除(或隐藏)页眉和页脚。我以为正确的方法会使用这种方法,但我想不出来 任何指点都将不胜感激(我对Word和VBA都是新手)Word VBA宏以插入文件和合并格式,vba,ms-word,Vba,Ms Word,我有一个带有页眉/页脚和文本格式的模板。我想写一个宏,用.rtf或.doc文件的内容填充这个模板。此外,我希望合并格式,以便保留模板文件的标题和格式,以及.rtf或.doc文件中的图片 剪切粘贴效果很好。如果我打开并保存模板文件,打开要插入的文件,选择“全部”,然后使用“合并格式”粘贴“特殊”,那么我得到的正是我想要的。我只是想要一个更具可扩展性的解决方案 我编写了一个宏来完成大部分工作,但它无法合并格式并删除(或隐藏)页眉和页脚。我以为正确的方法会使用这种方法,但我想不出来 任何指点都将不胜感
更新-我也尝试过,似乎使用了剪切和粘贴,但结果是一样的。以下是我能做的最好的。它可以粘贴为纯文本,但这总比什么都没有好(或者使用原始格式粘贴)
我在自己的VBA宏中尝试了相同的调用,发现 Selection.Range.InsertFile(FiletoInsert)
当我只传递一个参数文件名时,似乎可以工作。确保文件名完整。为什么没有其他有效答案时,有人不加评论地否决了我对自己问题的回答?
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
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
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