Notepad ++中的VBA SaveAs无法正常工作

时间:2016-12-01 13:01:19

标签: 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

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

1 个答案:

答案 0 :(得分:3)

您在nppl.SaveAs()之前关闭(alt + F4)。

但是 nppl.SaveAs无效,因为您无法像这样控制Notepad ++,您需要发送正确的密钥才能保存,这非常难看。

如果您只想保存文件:

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