Add documentname using oWord.Documents.Add? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Add documentname using oWord.Documents.Add?

26 Feb 2018, 05:13

Code: Select all

oWord := ComObjCreate("Word.Application")
oWord.Documents.Add
Is it possible to actually open a named document? Right now it's called "Document 1" or if I have open WOrd already Document X where X is the number followed by all the open docs. So I'd like to create a

"Mytitle" document. Is it possible?
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Add documentname using oWord.Documents.Add?

26 Feb 2018, 07:57

Yeah I've read that one and it's from there I am creating my script. But can't seem to find how to name a doc upon creating it. Neither did I find anything useful on MSDN.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Add documentname using oWord.Documents.Add?  Topic is solved

26 Feb 2018, 09:03

- Usually Word gives you a temporary document name, otherwise you give it a name when you save it.
- I wouldn't expect that you can change the temporary name, maybe you can.
- A workaround would be to create a blank file with a specific filename.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
gregster
Posts: 5390
Joined: 30 Sep 2013, 06:48

Re: Add documentname using oWord.Documents.Add?

26 Feb 2018, 09:13

There seem to be Save, SaveAs and SaveAs2 methods for different use cases.
https://msdn.microsoft.com/en-us/librar ... e.14).aspx
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Add documentname using oWord.Documents.Add?

26 Feb 2018, 09:39

jeeswg wrote:- Usually Word gives you a temporary document name, otherwise you give it a name when you save it.
- I wouldn't expect that you can change the temporary name, maybe you can.
- A workaround would be to create a blank file with a specific filename.
That's a bummer, yeah I guess I need to create a file first. Thanks!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Add documentname using oWord.Documents.Add?

26 Feb 2018, 10:46

- @gregster: Cheers for the link.
- @Johana: Yeah, I thought it was a pretty reasonable idea, if I'm working on some temporary unsaved files, specifying a name, for easier identification, wouldn't be a bad thing.

- I did give it a go, but it didn't work.
- For me it creates 'Document1', is that the same for other languages?
- I would often like to be able to change templates like this e.g. in Explorer: 'New folder' (new) versus the better 'New Folder' (old). But it never seems to be either possible or easy.
- Sometimes I change the window's title or icon, to help me distinguish between multiple instances of a program.

Code: Select all

q:: ;word - try to change name (doesn't work)
oWd := ComObjCreate("Word.Application")
oWd.Visible := -1 ;True
oWd.Documents.Add
MsgBox, % oWd.ActiveDocument.Name ;Document1
oWd.ActiveDocument.Name := "New Name" ;doesn't work
;error: 'Name' is a read only property.
MsgBox, % oWd.ActiveDocument.Name ;Document1
oWd := ""
return
- An example to open/save a file in Word. It expects a file called 'z word temp 1.docx' to exist on the Desktop.

Code: Select all

w:: ;word - open/save a file
vPath := A_Desktop "\z word temp 1.docx"
vPath2 := A_Desktop "\z word temp 2.docx"
if !FileExist(vPath)
	return
oWd := ComObjCreate("Word.Application")
oWd.Visible := -1 ;True
oWd.Documents.Open(vPath)
MsgBox, % oWd.ActiveDocument.Name ;z word temp 1.docx
if !FileExist(vPath2)
	oWd.ActiveDocument.SaveAs(vPath2)
oWd := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
BoBo
Posts: 3877
Joined: 13 May 2014, 17:15

Re: Add documentname using oWord.Documents.Add?

26 Feb 2018, 11:10

- For me it creates 'Document1', is that the same for other languages?
Of course not :shh: eg. it's Dokument1 in German.
Guest

Re: Add documentname using oWord.Documents.Add?

26 Feb 2018, 12:05

New document from template:
Documents.Add("C:\Template.dotx")

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, JAutohotkey, mikeyww, RaptorX and 38 guests