VBA另存为记事本++;不起作用

VBA另存为记事本++;不起作用,vba,notepad++,Vba,Notepad++,我有以下生成报告并将其复制到Notepad++的代码,但它只是没有根据需要保存它。有人能帮忙吗? 它不会生成错误代码 Sub Main Dim nppl stattempname = ActiveDocument.FullName stattempname = Replace(stattempname, ".pcb", "_STATS.txt") On Error Resume Next staFile = stattempname Kill staFile On Error GoTo 0

我有以下生成报告并将其复制到Notepad++的代码,但它只是没有根据需要保存它。有人能帮忙吗? 它不会生成错误代码

Sub Main

Dim nppl

stattempname = ActiveDocument.FullName
stattempname = Replace(stattempname, ".pcb", "_STATS.txt")

On Error Resume Next
staFile = stattempname
Kill staFile
On Error GoTo 0

Dim objData As New MSForms.DataObject
Dim strText As String

strText = stattempname

objData.SetText strText
objData.PutInClipboard

STATCommand = ""
STATCommand = STATCommand & "Application.ExecuteCommand(""Reports"")" & vbCrLf
STATCommand = STATCommand & "ReportsDlg.SelectReportFilesForOutput.Selected(1) = true" & vbCrLf
STATCommand = STATCommand & "ReportsDlg.Ok.Click()" & vbCrLf
Application.RunMacro "",STATCommand

On Error Resume Next
nppl = Shell("C:\Program Files (x86)\Notepad++\notepad++.exe")
AppActivate nppl
SendKeys "^+s", True
SendKeys "^v~", True
SendKeys "%{F4}", True
nppl.SaveAs FileName:="" & stattempname
End Sub
nppl.SaveAs()之前关闭(alt+F4)

但是
nppl.SaveAs
无效,因为您无法像那样控制记事本+,您需要发送正确的键来保存,这非常难看

如果您只想保存文件:

stattempname = "c:\null\somefile.TXT"
strText = "Hello World"

Dim hF As Integer: hF = FreeFile()
Open stattempname For Output As #hF
    Print #hF, strText
Close #hF
如果要在NPP中打开,只需在保存后打开:

Shell "C:\Program Files (x86)\Notepad++\notepad++.exe " & """" & stattempname & """", vbNormalFocus
nppl.SaveAs()之前关闭(alt+F4)

但是
nppl.SaveAs
无效,因为您无法像那样控制记事本+,您需要发送正确的键来保存,这非常难看

如果您只想保存文件:

stattempname = "c:\null\somefile.TXT"
strText = "Hello World"

Dim hF As Integer: hF = FreeFile()
Open stattempname For Output As #hF
    Print #hF, strText
Close #hF
如果要在NPP中打开,只需在保存后打开:

Shell "C:\Program Files (x86)\Notepad++\notepad++.exe " & """" & stattempname & """", vbNormalFocus

如果删除所有
On Error
-语句,是否会出错?为什么不直接从VBA写入文本文件?如果删除On Error语句,则不会出错。如果删除所有
On Error
-语句,是否会出错?为什么不直接从VBA写入文本文件?如果删除On语句,则不会出错错误语句。我将改为这样做。我将改为这样做。