PDA

View Full Version : Solved: Create new blank document on C Drive using QAT



hkeiner
10-08-2011, 09:45 AM
I am trying to write a sub (e.g., CreateNewDoc) that will create a new Word document with a particular name (e.g., 'NewDocument.docx') at a particular location on the C drive (e.g., 'C:\') without opening the document or requiring the user to respond to any dialog boxes. Further, if the 'NewDocument.docx' document already exists at the specified location, it would be automatically overwritten with the new document. If it is necessary to 'open' the new document, then perhaps an alternative is to create the document as "not visible" and then close it immediately via code. I already know how to add an icon to the QAT and link it to a public sub.

Said another way, I want to use a Quick Access Toolbar macro to create the new document on the hard drive without seeing anything appear on screen or needing to perform any other action.

I searched this forum and didn't see any posts that helped me figure this out. I am a bit of a newbee so perhaps I just didn't recognize any relevent posts.

Thanks in advance for any advice or exampe code.


Public Sub CreateNewDoc ()
' Code to create new document
End Sub

Frosty
10-12-2011, 02:52 PM
I'm not sure what you're asking... do you want help with your CreateNewDoc procedure? Or do you want help with the QAT concept and distribution of it to other users?

The QAT was designed with the idea that it is the area where users customize their own working area. In short... the mechanism to distribute QATs to multiple end-users is not simple. That's what, according to MS, the ribbon is for. Ribbon = company-wide macros. QAT = individual user customizations to optimize personal workflow.

Distributing a macro which is triggered by a button on the QAT to other users is fairly complicated, and there are a number of different strategies. I don't want to get into that discussion if you're simply asking how to write the macro.

However, this won't prompt, and will simply overwrite the file. I'm not sure why you want to do this, however... newly created blank documents overwriting existing documents is not, typically, a good way to do things.


Sub CreateNewDocument()
Dim oDocNew As Document
Dim sName As String
Dim sWhere As String

sName = "Hello"
sWhere = "C:\Temp"
Set oDocNew = Documents.Add(Visible:=False)
oDocNew.SaveAs2 FileName:=sWhere & Application.PathSeparator & sName
oDocNew.Close
End Sub

hkeiner
10-12-2011, 04:46 PM
Your code is exactly what I was looking for. Thanks.

For curiosity sake, a quick explanation for my 'unusual' need for this code is that I have other code on the QAT controlled sub that opens this particular file (in invisible mode) so that Word will always be running invisibly in the background. Word documents open more quickly on our PCs when Word is already running.

For a more detailed explanation, our department uses a document management system (client based application that creates and manages Word documents saved to a SQL server) and it automatically opens and closes multiple Word documents as certain procedures are performed. For example, through poor design (in my opinion) the document management system quits the Word application and then starts the Word application each time a user naviagates from one Word document to another. This causes a noticable delay to the user waiting for the Word application to start each time. I found that if the Word application is started and running 'outside' of the desktop managment system on the PC, the document management system does not quit this instance of Word and Word documents opened by the document management system opens much much quicker. Reqarding the QAT, I have only 5 users and I will 'install' the QAT icon on each PC myself and then put the linked sub on a shared template located on a shared server.

Frosty
10-12-2011, 05:03 PM
Hmm... sounds like your DMS is using GetObject to find an existing instance of Word to use it, and if it doesn't, then it uses CreateObject to start a new instance of Word. That's fairly standard practice... and if you use CreateObject, it is "polite" in the coding world to dismiss/quit the object you've created when you're done processing. Without knowing more, it certainly seems strange to continually quit the Word process during typical DMS actions (opening and closing documents).

Are you familiar with the concept of global addins? There maybe a slightly easier way to distribute your QAT button.

1. Create your .dotm template, and add the QAT button... but instead of adding for all documents (top right area of the Customize QAT dialog), change to For YourTemplate.dotm).

2. Save that template to your network share location.

3. Create a shortcut to that template, and put it in the Word Startup folder for each of your 5 users.

And your button will show up on the QAT.

Note: it will always show up to the far right side of the QAT... user customizations will take precedence (they start from the left and work their way right), but the button will be there.

This is also a way to have custom QAT buttons for specific templates (i.e., if you have a "Letter" template, when you have a document based on that template open.... you can see specific QAT buttons there).

I've uploaded a sample .docm, since .dotm attachments aren't allowed. But it should be saved as a .dotm and then follow the above steps.

This may save you some steps in the distribution process.

hkeiner
10-12-2011, 05:46 PM
Regarding the automatic quit, the Word (docm) documents created by the document management system have lots of VBA code attached that provides funtionality to the users of these documents. I suspect that quiting the Word application after the document is closed clears temporaty variables (and other code) from memory as an easy way to avoid conflicts with the code running from the next newly opened Word document. Just a guess...

Regarding the QAT, great suggestion and I am going to try implementing this. I am a bit of a newbee hack at VBA and I am just trying to make work better for my staff when I can figure out how to do that. I am actually an audit manager and only dabble with VBA code when I have a compelling reason to do so. Too much to learn to be proficient...

Frosty
10-12-2011, 07:08 PM
If you're right about the reasons, then you're also right about the poor design. There are a lot of ways to clear out temporary variables without having to quit the application. But that's just programmer grumbling.

Glad to have been of some help. Good luck! And remember, the skill is not already knowing the answers... the skill is in being able to find the answers. Probably over half the knowledgeable coders on this forum (me included) started as dabblers... and have simply been dabbling a bit longer than you have.

hkeiner
11-02-2011, 09:59 AM
Your suggestion on distributing the 'custom' QAT button(s) to all users using a shared template worked like a charm. Much better method than visiting each user's PC to add/change the custom QAT buttons(s). Thanks for the excellent suggestion.