简体   繁体   English

如何使用VBA从MS字单元中获取内容并将其清理为Excel

[英]How to get content from MS word cells with VBA and clean it for Excel

I am trying to get content from cells of a MS word document and copy it into an excel document with a macro using VBA. 我试图从MS Word文档的单元格中获取内容,然后使用VBA将其复制到带有宏的excel文档中。

I am using this function in order to achieve it: 我正在使用此功能来实现它:

Cells(insertRow, 1) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)

But this only brings the text with spaces, without respecting the line jumps (carriage returns). 但这只会使文本带有空格,而不会考虑行跳(回车)。

I have also tried to use both of this: 我也尝试使用这两个:

Cells(insertRow, 1) = WorksheetFunction.Clean(.cell(iRow, iCol).Range)

And

Cells(insertRow, 1) = WorksheetFunction.Clean(.cell(iRow, iCol))

With the same result. 结果相同。

That would be the main task. 那将是主要任务。 I would also like to know if it is possible to change this symbol from MS word into something similar in excel. 我也想知道是否有可能将此符号从MS单词更改为excel中的类似符号

You could try adding a symbol at the end of each line in the MS word file so as to recover the line jumps. 您可以尝试在MS Word文件的每行末尾添加符号,以恢复行跳转。 For exemple, if you finish your lines by a semicolon, you could ask Excel to look for the semicolon, and when it finds it, skip a line. 例如,如果用分号结束行,则可以要求Excel查找分号,并在找到分号后跳过一行。

To remove the spaces you could consider runing this code : 要删除空格,您可以考虑运行以下代码:

newString = Replace(strString, " ", "")

There may be other ways to achieve what you wish to do, but this feels like the simplest way. 可能还有其他方式可以实现您想要的目标,但这似乎是最简单的方式。 Hope this helped! 希望这对您有所帮助!

It took me a long while to figure out how to do this, as it was my first time programming in Visual Basic and VBA, but this is how i solved it: 我花了很长时间才弄清楚该如何做,这是我第一次使用Visual Basic和VBA进行编程,但这就是我解决的方法:

The first problem i had was that i had some junk characters i had to get rid of, which i tried using 我遇到的第一个问题是我不得不摆脱一些垃圾字符,我尝试使用这些字符

WorksheetFunction.Clean(MyString)

Problem is this deletes non printing characters which are imported (ASCII code 0-31) which included ascii code 13 (line jump or carriage return). 问题是这会删除导入的非打印字符(ASCII代码0-31),其中包括ascii代码13(行跳转或回车)。

Even without using WorksheetFunction.clean line jump was not correctly interpreted by excel, which i solved using this 即使不使用WorksheetFunction.clean,Excel也无法正确解释行跳转,我使用此方法解决了该问题

Dim str as String
Dim clearer As String
str=.cell(iRow, iCol).Range.Text 'Get non treated content from word table cell
clearer = Replace(str, Chr(13), vbNewLine)

Now that i was not cleaning automatically all stuff, i had to do it manually, but i did not know the Ascii code of strange characters. 既然我没有自动清理所有东西,我不得不手动进行,但是我不知道奇怪字符的Ascii代码。

I looked for them this way 我这样找他们

 Dim Counter As Integer
MsgBox ("Word " + str)
For Counter = 1 To Len(str)
MsgBox ("Letter is " + Mid(str, Counter, 1) + "And Ascii code is" + 
 CStr(AscW(Mid(str, Counter, 1))))
Next

Once I knew their code, the process for manual cleaning was the same for other characters. 一旦知道了他们的代码,其他字符的手动清理过程就相同。 Some of them represented a line_jump and some others were junk, so i treated them in a different way 其中一些代表line_jump,另一些则代表垃圾,因此我以不同的方式对待它们

clearer = Replace(str, Chr(13), vbNewLine) 'Replace line jumps from word to excel
clearer = Replace(clearer, Chr(11), vbNewLine) 'Replace line jumps from word 
to excel
clearer = Replace(clearer, Chr(7), "") 'Remover char

This way, i could clean the imported text as i wanted. 这样,我可以根据需要清除导入的文本。 Hope this can help someone in the future. 希望这可以在将来对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM