使用vba将文本从单词复制到Excel单元格

时间:2017-07-28 10:00:17

标签: html excel vba ms-word ms-office

我有关于vba的问题。我试图将word文档中的文本内容复制到excel表中,然后复制到特定单元格。现在,我可以将内容复制为单个字符串,但我希望保持格式化。我尝试过使用以下内容:

ActiveSheet.PasteSpecial Format:="Microsoft Word 8.0 Document Object"

但遗憾的是,它只复制了文件的图像(虽然在正确的单元格中)。

有没有办法用vba做到这一点?有数百份文件,因此手工完成是不可行的。

或者,如果这是不可能的,我也很高兴,如果文件被粘贴为带有换行符和间距指示的html。

感谢您的帮助!

编辑28 / 7,13:18:

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("C:\afloder")
i = 10

'loop through each file in the directory and prints their names
For Each objFile In objFolder.Files
    'print file name
    temp = CStr(objFile.Name)
    splittedTemp = Split(temp, ".")
    Cells(i + 1, 1) = splittedTemp(0)

    'paste in the content
    Doc = CStr("C:\afolder\" & objFile.Name)
    Set WordDoc = Word.Documents.Open(Doc)
    Word.ActiveDocument.Content.Copy

    'Call the paste procedure
    Worksheets("Communication Template").Cells(i + 1, 9).Select
    DoEvents
    ActiveSheet.PasteSpecial Format:="Microsoft Word 8.0 Document Object", DisplayAsIcon:=False

    WordDoc.Close savechanges:=False
    Set WordDoc = Nothing

    i = i + 1
Next objFile

Set WordApp = Nothing

0 个答案:

没有答案