vba - 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©ç
€¸,—&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.
解决方案
您正在尝试读取 docx 或 docm 文件,这是一个 zip 存档。Word 文件不是纯文本文件,因此您不会得到任何有意义的东西来对待它们。您需要使用 Word 或其他可以读取此类文件的应用程序打开该文件。
推荐阅读
- python - 将两个数组相乘并求结果的总和
- python - 为什么我收到“DLL 加载失败:%1 不是有效的 Win32 应用程序”错误?
- c - 链表销毁后,使用链表的斐波那契数列崩溃。如何跟踪错误?
- microsoft-cognitive - 在具有范围值的研究名称字段上使用 CalcHistogram 不会返回结果
- php - 从动态文本框sqlserver多次插入
- socrata - Sodapy HTTPError:403 客户端错误:禁止
- python - Keras 是否具有将输入词向量复制并反向传播到仅一组的功能?
- javascript - Angular 应用程序中的自定义非 Angular JavaScript
- html - 单击菜单时的悬停效果
- sql-server-2008 - 从每组中选择前 N 行