将Word文档文件另存为文本文件在Excel VBA中,并出现运行时错误438。

huangapple go评论84阅读模式
英文:

saving a word docx file as a txt in excel VBA and getting runtime error 438

问题

我正在尝试使用Excel VBA将Word文档(.docx文件)另存为文本文件(.txt文件)。然而,当我尝试保存为新的字符串时,出现错误438。

Sub SaveToTXT()

    Dim wordDoc As Object
    Set wordDoc = CreateObject("Word.Application")
    
    Dim wordPath As String
    
    wordPath = "C:\Users\file"
    
    wordDoc.Documents.Open wordPath
    
    wordDoc.Visible = False
    
    wordPath = Replace(wordPath, ".docx", ".txt")
       
    wordDoc.SaveAs2 (wordPath)
    
    wordDoc.Quit

End Sub

希望这有所帮助。

英文:

I am trying to save a word docx file as a txt using excel VBA. However the error 438 comes up when i try to save as the new string

Sub SaveToTXT()

    Dim wordDoc As Object
    Set wordDoc = CreateObject("Word.Application")
    
    Dim wordPath As String
    
    wordPath = "C:\Users\file"
    
    wordDoc.Documents.Open wordPath
    
    wordDoc.Visible = False
    
    wordPath = Replace(wordPath, ".docx", ".txt")
       
    wordDoc.SaveAs2 (wordPath)
    
    wordDoc.Quit

End Sub

答案1

得分: 1

这是一种不好的想法,当你刚开始学习VBA时,不要使用晚期绑定,因为晚期绑定不提供智能感知。设置对Ms Word的引用,以便获得完整的智能感知(在IDE中使用工具 > 引用,然后确保勾选MS Word xx.x对象库)。

下面的代码应该让你更接近你想要的:

Sub SaveToTXT()

    Dim myword As Word.Application
    Set myword = New Word.Application
    
    Dim myPath As String
    
    ' 将##user##更改为你的用户名
    myPath = "C:\Users\##user##\Documents\"
    
    Dim myDoc As Word.Document
    Set myDoc = myword.Documents.Open(myPath)
    
    myWord.Visible = False
      
    myDoc.SaveAs2 Replace(myDoc.Name, ".docx", ".txt"), fileformat:=wdFormatText
    
    myDoc.Close savechanges:=False
    
    myword.Quit
    Set myword = Nothing

End Sub

如果你是VBA初学者,我还强烈建议安装免费且出色的Rubberduck插件,以进行VBA代码检查。

英文:

Its a bad idea to use late binding when you are new to VBA because late binding doesn't give you any intellisense. Set a reference to Ms word so that you can get full intellisense (In the IDE use Tools.References then make sure that MS Word xx.x object library is ticked).

The code below should get you closer to what you want

Sub SaveToTXT()

    Dim myword As Word.Application
    Set myword = New Word.Application
    
    Dim myPath As String
    
    ' change ##user## to you username
    myPath = "C:\Users\##user##\Documents\"
    
    Dim myDoc As Word.Document
    Set myDoc = myword.Documents.Open(myPath)
    
    myWord.Visible = False
      
    myDoc.SaveAs2 Replace(myDoc.Name, ".docx", ".txt"), fileformat:=wdFormatText
    
    myDoc.Close savechanges:=False
    
    myword.Quit
    Set myword = Nothing

End Sub

If you are a beginner in VBA then I'd also strongly recommend installing the free and fantastic Rubberduck addin for VBA and pay attention to the code inspections.

huangapple
  • 本文由 发表于 2023年4月4日 03:37:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定