How to create and name a new document file(.docs) in a selected folder?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
dtmed330
Posts: 2
Joined: 23 Mar 2022, 02:06

How to create and name a new document file(.docs) in a selected folder?

23 Mar 2022, 02:13

Hi, there:

I'm trying to create a new word file with a predefined name in a specific folder. I used the following script but fail to open the file. Could anyone help deal with this? Thank you a million.

Ryan Huang

Code: Select all

#!w::
; create new word file in current window focus
#IfWinActive ahk_class CabinetWClass
>!w:: ;explorer - create new word file and focus/select it
#IfWinActive ahk_class ExploreWClass
>!w:: ;explorer - create new word file and focus/select it
;note: similar to: right-click, New, Word Document
vNameNoExt := "Comments"
vDotExt := ".docx"
WinGet, hWnd, ID, A
for oWin in ComObjCreate("Shell.Application").Windows
{
	if (oWin.HWND = hWnd)
	{
		vDir := RTrim(oWin.Document.Folder.Self.Path, "\")
		;if !DirExist(vDir)
		if !InStr(FileExist(vDir), "D")
		{
			oWin := ""
			return
		}

		Loop
		{
			vSfx := (A_Index=1) ? "" : " (" A_Index ")"
			vName := vNameNoExt vSfx vDotExt
			vPath := vDir "\" vName
			if !FileExist(vPath)
				break
		}

		;create a blank word file (ANSI/UTF-8/UTF-16)
		;FileAppend,, % "*" vPath
		FileAppend,, % "*" vPath, UTF-8
		;FileAppend,, % "*" vPath, UTF-16

		;SVSI_FOCUSED := 0x10 ;SVSI_ENSUREVISIBLE := 0x8
		;SVSI_DESELECTOTHERS := 0x4 ;SVSI_EDIT := 0x3
		;SVSI_SELECT := 0x1 ;SVSI_DESELECT := 0x0
		Loop 30
		{
			if !(oWin.Document.Folder.Items.Item(vName).path = "")
			{
				oWin.Document.SelectItem(vPath, 0x1F)
				break
			}
			Sleep, 100
		}
		break
	}
}
oWin := ""
return
#IfWinActive
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 23 Mar 2022, 02:37, edited 1 time in total.
Reason: Please use code tags, next time. Thank you!
User avatar
mikeyww
Posts: 27315
Joined: 09 Sep 2014, 18:38

Re: How to create and name a new document file(.docs) in a selected folder?

23 Mar 2022, 06:14

Code: Select all

fn = comments.docx

#IfWinActive ahk_class CabinetWClass
#!w::              ; WIN-ALT-W = Create and save a new Microsoft Word document
For each, dir in getSelected()
 If Instr(FileExist(dir), "D")
  wordCreate(dir "\" fn)
 Else MsgBox, 48, Error, Not a directory: %dir%
Return
#IfWinActive

wordCreate(file) { ; Create and save a new Microsoft Word document
 SplitPath, file,, dir
 If FileExist(file) {
   MsgBox, 48, Error, File already exists. Aborting.`n`n%file%
   Return
 } Else If !FileExist(dir) {
  FileCreateDir, %dir%
  If ErrorLevel
   MsgBox, 48, Error, An error occurred while creating the directory. Aborting.`n`n%dir%
 }
 SoundBeep, 1500
 oWord := ComObjCreate("Word.Application") ; https://www.autohotkey.com/boards/viewtopic.php?f=7&t=8978
 oWord.Documents.Add(), oWord.Visible := True, oWord.Activate()
 oWord.Application.ActiveDocument.SaveAs(file)
}

getSelected() {    ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
                   ; Adapted from teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
 Return selection
}
dtmed330
Posts: 2
Joined: 23 Mar 2022, 02:06

Re: How to create and name a new document file(.docs) in a selected folder?

23 Mar 2022, 08:46

Dear mikeyww:

Thank you so much for answering my question.
However, the script doesn't seemingly work well.
Here are the situations.
When I activated the hotkey in a blank folder, no response was observed.
And when I activated the hotkey while selecting a blank folder, the error msg was "File already exists. Aborting."
I can't find a solution to fix it. HELP!

Ryan
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: How to create and name a new document file(.docs) in a selected folder?

23 Mar 2022, 09:25

How about copying and renaming the empty word file you have stored somewhere on disk?
User avatar
mikeyww
Posts: 27315
Joined: 09 Sep 2014, 18:38

Re: How to create and name a new document file(.docs) in a selected folder?

23 Mar 2022, 09:42

If the file already exists, then it was created, right? I'm not sure why yours did not appear, if it did not already exist.

You could remove the #If... directive, in case that line was an issue. Did you hear the beep?

Yes, copying a template sort of file is a simple alternative with FileCopy.
User avatar
flyingDman
Posts: 2844
Joined: 29 Sep 2013, 19:01

Re: How to create and name a new document file(.docs) in a selected folder?

23 Mar 2022, 12:12

What about this?:

Code: Select all

#\::
flnm := "comments.docx"
if !(path := getpath()) or (fileexist(path "\" flnm))
	return
oWord := ComObjCreate("word.application")
oWord.Documents.Add
oWord.Visible := True
oword.activeDocument.SaveAs(path "\" flnm)
return

getpath() {
	WinGetClass class, % "ahk_id " hwnd := WinExist("A")
	if (class ~= "(Cabinet|Explore)WClass")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
				return window.Document.Folder.Self.Path
	}
14.3 & 1.3.7
User avatar
michaelbeijer
Posts: 83
Joined: 04 Oct 2014, 08:01
Location: Hastings, UK.
Contact:

Re: How to create and name a new document file(.docs) in a selected folder?

20 Jan 2024, 14:14

flyingDman wrote:
23 Mar 2022, 12:12
What about this?:

Code: Select all

#\::
flnm := "comments.docx"
if !(path := getpath()) or (fileexist(path "\" flnm))
	return
oWord := ComObjCreate("word.application")
oWord.Documents.Add
oWord.Visible := True
oword.activeDocument.SaveAs(path "\" flnm)
return

getpath() {
	WinGetClass class, % "ahk_id " hwnd := WinExist("A")
	if (class ~= "(Cabinet|Explore)WClass")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
				return window.Document.Folder.Self.Path
	}
Thanks!
Michael Beijer
Dutch-English technical translator
Hastings, United Kingdom
Email: michael[at]beijer.uk
Website: Beijer.uk
Terminology: Beijerterm.com
AHK-learning project: Beijer.bot
Proz profile: proz.com/profile/652138
LinkedIn: linkedin.com/in/michael-beijer

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], just me, newbieforever, vmech and 119 guests