VBA Save Notepad File

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

VBA Save Notepad File

Post by MSingh »

Hi,

My .xlsm pastes a range to a .txt file.
What code can i use to prompt the user to rename & save the .txt file?

Thanks again
Mohamed

User avatar
HansV
Administrator
Posts: 78453
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Save Notepad File

Post by HansV »

What code are you using now?
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78453
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Save Notepad File

Post by HansV »

Without knowing the code that you use, I can only provide an example of how to ask the user for a name:

Code: Select all

Dim strName As String
strName = Replace(ActiveWorkbook.FullName, ".xlsm", ".txt")
strName = Application.GetSaveAsFilename(strName, "Text files (*.txt),*.txt")
If strName = "False" Then
  MsgBox "You didn't specify a filename!", vbExclamation
  Exit Sub
End If
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: VBA Save Notepad File

Post by MSingh »

Hi Hans,

Didn't see your question in time.
Currently using this code to paste to notepad:

Sub Copy_To_Notepad ()

Range("e:e").Copy
RetVal = Shell("C:\WINDOWS\notepad.exe C:\Data V1.0\Data Batch Clearing\Data Import.txt", 1)
SendKeys "^v"
SendKeys "^a"
SendKeys "^c"
MsgBox "In NotePad Go To File: SAVE AS & Name the NotePad file" & vbCrLf & _
vbCrLf & _
"as per the Order Number.", vbOKOnly, "Save File"

End Sub

Second Question:

When this pastes to notepad the pasted range is highlighted/selected.
Is there a way to deselect in notepad with vba?

Thanks again
Mohamed

User avatar
HansV
Administrator
Posts: 78453
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Save Notepad File

Post by HansV »

The line

SendKeys "^a"

selects all text in the Notepad file. If you omit this line (and the next, which isn't used), the text will not be selected.
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: VBA Save Notepad File

Post by MSingh »

Thank you Hans.