2016-12-01 21 views
0

我有以下代碼生成報告並將其複製到Notepad ++,但它不會根據需要保存它。任何人都可以幫忙嗎? 它沒有生成錯誤代碼。VBA另存爲在記事本+ +不起作用

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

你得到任何錯誤,如果你刪除所有'On Error'語句? – Verzweifler

+1

爲什麼不直接從VBA編寫文本文件? – Comintern

+0

如果刪除On Error語句,則不會出現錯誤。 – Dan

回答

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 
+0

我會這樣做。 – Dan