vba Word 宏另存为带有当前文件名的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11470750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Word Macro Save as Text w/ Current File Name
提问by user1440061
So what I am trying to do is take a word document, save it as a plaint text .txt with the line breaks enabled. I was told a macro would be an easy way to do this, and I've had some success. My biggest problem right now is that it is not saving the document as it's current file name. I looked around and people have said to use ActiveDocument.Name, but for some reason that's not working for me and I end up with the document saved as a txt file but literally named ActiveDocument.Name. This is my first attempt at anything VBA related, so it's probably some simple little syntax error that I can't see. Here's my current code:
所以我想要做的是获取一个 word 文档,将其保存为纯文本 .txt 并启用换行符。有人告诉我,使用宏是一种简单的方法,而且我已经取得了一些成功。我现在最大的问题是它没有将文档保存为当前文件名。我环顾四周,人们说要使用 ActiveDocument.Name,但由于某种原因,这对我不起作用,我最终将文档另存为 txt 文件,但实际上命名为 ActiveDocument.Name。这是我第一次尝试任何与 VBA 相关的东西,所以它可能是我看不到的一些简单的小语法错误。这是我当前的代码:
Sub WordtoTxtwLB()
'
' WordtoTxtwLB Macro
'
'
ActiveDocument.SaveAs2 FileName:= _
"\Path\Path\FILENAME.txt", FileFormat:= _
wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, Encoding:=1252, InsertLineBreaks:=True, AllowSubstitutions:=False, _
LineEnding:=wdCRLF, CompatibilityMode:=0
End Sub
Note: I did change the path and file name in my code to generic ones (i.e. 'Path' and 'FILENAME') just to make things a little easier/clearer. SO if I had a document called cat.doc, I would want the macro to save it as a .txt file with line breaks and with the same file name. Any help/suggestions?
注意:我确实将代码中的路径和文件名更改为通用名称(即“路径”和“文件名”),只是为了使事情更容易/更清晰。因此,如果我有一个名为 cat.doc 的文档,我希望宏将其保存为带有换行符和相同文件名的 .txt 文件。任何帮助/建议?
回答by ForEachLoop
The name FILENAME is within quotes, so that it'll be used as a literal. Separate it out of the quotes to be used as a variable. Also, if you use ActiveDocument.Name, it'll have the suffix as part of the string ("somefile.doc"); you'll have to remove the suffix.
名称 FILENAME 在引号内,因此它将用作文字。将其与要用作变量的引号分开。此外,如果您使用 ActiveDocument.Name,它会将后缀作为字符串的一部分(“somefile.doc”);你必须删除后缀。
Sub WordtoTxtwLB()
'
' WordtoTxtwLB Macro
'
'
Dim fileName As String
myFileName = "New_File"
ActiveDocument.SaveAs2 FileName:= _
"\Path\Path\" & myFileName & ".txt", FileFormat:= _
wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, Encoding:=1252, InsertLineBreaks:=True, AllowSubstitutions:=False, _
LineEnding:=wdCRLF, CompatibilityMode:=0
End Sub