0

I have a VBA userform to open a text file in TextPad with the following:

Call Shell("C:\Program Files\TextPad 5\TextPad.exe " & "C:\testfile.txt", vbNormalFocus)

I'd like the text file to be displayed at a specific line number.

Is there a way to pass the line number as an argument to this Shell call?

Community
  • 1
  • 1
Excellll
  • 5,609
  • 4
  • 38
  • 55

2 Answers2

2

You can accomplish this using WScript.Shell.SendKeys to trigger the goto line shortcut within TextPad (ctrl-G)

Dim wshshell
Set wshshell = CreateObject("WScript.Shell")

Call Shell("C:\Program Files\TextPad 5\TextPad.exe " & "C:\testfile.txt", vbNormalFocus)
Application.Wait (10)
wshshell.SendKeys "^g"      'ctrl-G
Application.Wait (10)
wshshell.SendKeys "15"      'desired line number
Application.Wait (10)
wshshell.SendKeys "{ENTER}" 'enter

You might have to mess with the Wait lines to add or remove delays between commands, but I tested this from within a VBA module in Excel and it worked.

I am aware that SendKeys that can be called directly from VBA, but when I tried to use that, it seems to be bound to the vba editor window.

mr.Reband
  • 2,434
  • 2
  • 17
  • 22
  • Wow, this is great! I'm glad to see this method has even more flexibility than what I asked for because it will be easier for me to use the TextPad Find tool to get the right line. – Excellll May 14 '13 at 20:55
1

You can also call notepad++ with the "-n" argument for the same effect,

Sending keys (specially with delays) might be a dangerous thing if you switch screens or a popup window appears while the command is executing you might lose data or inadvertly execute a dangerous action in that other application.