首页 > 解决方案 > How to read a single line out of a word document

问题描述

The method I used was for text files and gives gibberish as expected.

In: John Smith
Out: PK!~8ìz‡­[Content_Types].xml ¢( ´”ÏNÂ@Æï&¾C³WÓ.x0ÆP8•D|€a;…Õvw³;ü{{§´5@UôBRf¾ß÷Ív;½Áº,¢%ú ­IE7鈲™6³T¼Lâ[“Aa
¦bƒAú—½ÉÆaˆXmB*æDîNÊ æXBH¬CÕÜúˆýL:Po0CyÝéÜHe
¡¡˜*†è÷†˜Ã¢ h´æ¿ë$SmDt_÷UV©ç
­€¸,—&amp;KÊÛ<×

I'm a novice at VBA, and I'm trying to read a document line by line so that I can eventually have the macro automatically remove entire lines based on their content.

Sub ayaya()
Dim TextLine As String
Open ActiveDocument.Path & "\Doc1.docm" For Input As #1
    Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, TextLine ' Read line into variable.
    Debug.Print TextLine
    Loop
    Close #1


End Sub

Part of me hoped that it would give "John Smith". I've seen some solutions put the entire document into a text file. Is there any way where I can delimit the data somehow? I'd like to be able to isolate a single line and remove it.

标签: vbams-word

解决方案


您正在尝试读取 docx 或 docm 文件,这是一个 zip 存档。Word 文件不是纯文本文件,因此您不会得到任何有意义的东西来对待它们。您需要使用 Word 或其他可以读取此类文件的应用程序打开该文件。


推荐阅读