2014-09-19 10 views
-2

다음은 Excel (vba)을 통해 하위 탭이 메모장을 열고 텍스트를 추가 한 다음 파일 이름으로 저장하라는 메시지가 표시되는 업데이트 된 예제입니다. vba 코드에서 Windows 저장 파일 대화 상자로 파일 이름을 전달하는 것을 제외하고는 작동합니다.Windows 파일 저장 대화 상자에서 파일 이름 설정

Option Explicit 
    Private Declare Function AllowSetForegroundWindow Lib "user32.dll" (ByVal dwProcessId As Long) As Long 
    Private Declare Function BringWindowToTop Lib "user32" (ByVal lngHWnd As Long) As Long 
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpszClass As String, ByVal lpszTitle As String) As Long 
    Private Declare Function LockSetForegroundWindow Lib "user32.dll" (ByVal uLockCode As Long) As Long 
    Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long 
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long 
    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) 

    Private Const WM_SETTEXT As Long = &HC 
    Private Const LSFW_LOCK = 1 
    Private Const VK_CONTROL = &H11 '0x11 
    Private Const VK_S = &H53  '0x53 

Sub WriteToNotepad() 
    Dim hwndNotepad&, hwndTextbox&, hwndSaveAs&, hwndSaveLocation, hwndFileName&, Retval 

ResumeHere: 
' Start "Notepad" 
    Retval = Shell("C:\Windows\System32\NotePad.exe", 4) 

' Identify handle for "Notepad" window 
    hwndNotepad = FindWindowEx(0, 0, "Notepad", vbNullString) 
    hwndTextbox = FindWindowEx(hwndSaveAs, 0, "Edit", vbNullString) 

' Write message 
    SendMessageString hwndTextbox, WM_SETTEXT, 0, "My message goes here" 

' Lock the window for futher input 
    BringWindowToTop (hwndNotepad) 
    AllowSetForegroundWindow (hwndNotepad) 
    SetForegroundWindow (hwndNotepad) 
    LockSetForegroundWindow (LSFW_LOCK) 

' Show Save As dialog box 
'Press Ctrl key down, but don't release 
    keybd_event VK_CONTROL, 0, 0, 0 
'Press the letter "S" then release 
    keybd_event VK_S, 0, 0, 0 
    keybd_event VK_S, 0, 2, 0 
'Release the Alt key 
    keybd_event VK_CONTROL, 0, 2, 0 

' Find SaveAs window before continuing 
    hwndSaveAs = FindWindowEx(0, 0, "#32770", vbNullString) 
    hwndFileName = FindWindowEx(hwndSaveAs, 0, "Edit", vbNullString) 

' Write file name 
    SendMessageString hwndFileName, WM_SETTEXT, 0, "Testing file.txt" 

End Sub 
+1

귀하의 코드는 어디에 있습니까? – Raptor

+0

[GetSaveFileName()] 가능한 중복 "파일 이름 :"컨트롤에서 파일 확장명을 업데이트하는 방법?] (http://stackoverflow.com/questions/14449280/getsavefilename-how-to-update-the-file-extension -in-the-file-name-control) –

+0

Raptor와 Remy에게 감사드립니다. 새로운 예제 코드가 질문에 추가되었습니다. 나는 그것이 레미 이전에 작업했던 것과 중복 된 질문이라고 생각하지 않지만 오해를 불러 일으킬 수 있습니다. – abousetta

답변

2

글쎄, 당신은 분명히 키 입력을 합성하지 않아도됩니다. 저장 (또는 열기) 대화 상자에서 파일 이름 필드를 미리 채우는 올바른 방법은 OPENFILENAME structure 멤버에 원하는 문자열을 넣은 다음 GetSaveFileName function에 전달하는 것입니다.

사용자가 대화 상자를 닫으면 해당 필드가 선택한 파일 이름과 경로로 업데이트됩니다.

+0

감사합니다. Cody. 첫 번째 예에서 더 명확하지 않은 것에 대해 사과드립니다. 나는 내가하고있는 것보다 더 명확하게 바라는 새로운 예를 만들었다. – abousetta