Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro for saving Word.docx with text of Header of that document 1

Status
Not open for further replies.

Koen Piller

Programmer
Jun 30, 2005
841
0
0
NL
Hi,
I am new in Macro-country for MSWord.
Would like to create a macro which would save my MSWordfile with a filename which is the same as the Documentheader. If that than works I would like to add a custom icon to my preferable list activating this macro. Is that doable? And if so, someone willing to give me a hand?
Stay Healthy,
Koen
 
How is it that this document has a header but hasn't been saved?
How many paragraphs are in the header, & which one do you want to use?
Is there any content other than plain text in the header (e.g. tabs, images, etc.)?
Which header do you want to capture from (Word documents have three header objects)?

Cheers
Paul Edstein
[MS MVP - Word]
 
Paul,
Upon creation of the document the first thing is, in this business rule, to make a header. The header contains only one paragraphv and only text, no pictures. The header is for all pages / paragraphs of the document. The header contains only one column and is pure text.
This, further blank document is now saved as follows:
after creating the header, the content is C&P. The file is saved and in the box filename the header is C&V.
This all works fine untill you, have to change the header for some reason. Then you will end up with 2 saged docs where the first is saved with a wrong name.
Would be handy to have a button SaveWithHeaderContent without this C&P and C&V
Would be nice if the macro replaces the old filename with the new filename
In case there is no headercontent and user saves the file than one can type in the filename in the box.
Does it sound logical and moreover is this doable, to make such a button?
Stay healthy,
Koen
 
You could add a macro like the following to the document template's 'This Document' code module:
Code:
Sub FileSave()
Dim StrName As String, StrPath As String, i As Long
Const StrNoChr As String = """*./\:?|"
With ActiveDocument
  StrName = Trim(Split(.Sections.First.Headers(wdHeaderFooterPrimary).Range.Text, vbCr)(0))
  If StrName = Split(.Name, ".doc")(0) Then Exit Sub
  StrPath = .Path
  If StrPath <> "" Then StrPath = StrPath & "\"
End With
For i = 1 To Len(StrNoChr)
  StrName = Replace(StrName, Mid(StrNoChr, i, 1), "_")
Next
With Application.Dialogs(wdDialogFileSaveAs)
  .Name = StrPath & StrName
  .Show
End With
End Sub
The above macro will intercept save actions without the need for a button. If the header has changed, whatever is in the updated header will be offered as the SaveAs filename. Illegal filename characters (and periods) are replaced with underscores.

Cheers
Paul Edstein
[MS MVP - Word]
 
Paul,
two things: like I said in my 1st message "I am new in Macro-country for MSWord" so where do I find the 'This Document module' and also I believe that the macro should be activated/present when I open a new Document, how can I be sure of that?
It was my intention just to open a new-blank MSWord document, set the Heading and click on the (newly added) icon which activates a macro to save the document as a file with name = Header content.
Is that feasable?
Stay healthy,
Koen
 
For general PC macro installation & usage instructions, see:
To make the macro available to all documents based on the template you're using for this, add the macro to the template's 'This Document' code module. To do that, open an existing document based on the template, or create a new one, then press Alt-F11 to open the VBA Editor. In the (upper) left panel, you'll see you'll see a reference to your document and, above that, a reference to its template. Click on the 'Microsoft Word Objects' entry to expand it, then click on the 'This Document' code module to open that. Copy the macro code from the post above and paste it into the 'This Document' code module. Press Alt-F11 to open the VBA Editor, then exit Word, remembering to allow it to save the changes to the template. Note that, if your template had previously been saved in the .dotx format, it will now need to be saved in the .dotm format (i.e. as a macro-enabled template).

There is no practical way to have Word save the document immediately you type something into the page header. As the very least, you'd have to exit the header and, moreover, the code to intercept such actions is much more complex and could impose a significant performance hit. Hence my writing of the code to intercept the File Save process.

Cheers
Paul Edstein
[MS MVP - Word]
 
> the code to intercept such actions is much more complex and could impose a significant performance hit

Not sure I agree, you only really need to respond to the WindowSelectionChange event
 
Strongm,
Interesting, would you be able to construct such a macro for me:
save the file as (this.file header caption)
and than
assign that macro to a button which I could than add to my fast-bar ( the bar where you place your own preference buttons, right above the Start tab.

would be great!
Regards,
Stay healthy,
Koen
 
you only really need to respond to the WindowSelectionChange event
Precisely. Which entails a whole lot more coding than the macro I've supplied and would slow Word down by firing every time a selection is changed.

Cheers
Paul Edstein
[MS MVP - Word]
 
assign that macro to a button which I could than add to my fast-bar
Like the macro I supplied, WindowSelectionChange event macros run automatically - you don't assign buttons to them. The 'fast-bar', as you call it, is the Quick Access Toolbar (QAT).

Cheers
Paul Edstein
[MS MVP - Word]
 
Yep, I agree that what the OP really wants/needs is a button with a simple bit of code as per your posts.

However, I still differ from you - accepting this may just be a personal view - about the complexity and performance hit of code to monitor switching from in and out of the header (but will reiterate, such a piece of code does not seem to me appropriate as a solution for the OP); on my machine activating the relevant event handler when switching the selection from header to footer (worst case scenario from my testing) adds an overhead of about 1.5ms - which compared to the speed of the actual selection changing (about 28ms on my machine for this scenario), or user clicking or typing (we humans are so slow) it can pretty much be ignored. And remember it is only triggered when the selection changes, not every keypress (sure, clearly if you put lots and lots of slow code that is called for every single selection change you would see some sort of slow down - but that's not what I'd propose)
 
Mr./Mrs. Strongm
The piece of code mr./mrs. Macropod may work, but believe me this is not what I am looking for.
To activate that code I shall have to constuct a template with that macro.
Each and everytime I would like to have this code working I shall first have to open that template, correct me if I am wrong.
I just want to open a new-blank MS-word form and have an extra button on my QAT which will, instead of the default safe-as function, saves the form as the text given in the header.
It that is not possible, so be it, and I will have to continue my working with C&V the header, save-as, C&P in the 'filename' box.
This works fine, untill I have to change something in the header, in that case I end up with two versions, which is annoying.
So if you can construct a macro-button for me it would be fine.
Best regards,
Stay healthy,
Koen Piller
 
...

stick the macro in normal.dotm
create your button in the QA menu, and point it at the macro

job done

there are other ways, but this is pretty much the simplest
 
To activate that code I shall have to constuct a template with that macro.
Every Word document is based on a template. You cannot avoid that. Documents with the specific requirements yours have should be based on their own template, which need be nothing more than the document with its boilerplate content saved as a template. Hardly something difficult to do.
Each and everytime I would like to have this code working I shall first have to open that template, correct me if I am wrong.
You are wrong. All you need do to create a document from a template is to double-click on it, for example. Word will then create a new document from the template without you having to open the template, as such.
I just want to open a new-blank MS-word form and have an extra button on my QAT which will, instead of the default safe-as function, saves the form as the text given in the header.
Except for the quite unnecessary button, the code I provided does essentially what you asked.

It seems to me you're more concerned with finding fault with a solution that differs from what you asked for, regardless of the fact the solution is better in many respects, and without regard for the way Word actually works. One wonders if you even bothered to try the macro before deciding to 'find' fault with it.

Cheers
Paul Edstein
[MS MVP - Word]
 
Paul,
I feel extremely sorry, it seems you find me quit stubborn. Which I would very much apologize for.
If I open my Word (2010) - I click on the shortcut which is placed on by desktop, since I was 'playing' with the macro you supplied, following happens: a yellow container opens with : "Sequtiry warning", macro's are disabled plus a butten with "enabled content".
I click on that button. I create a heading and click on Save, this will prompt me with a filename: "Doc1" in the default location as specified in my the Save section of my Settings.
Maybe I have not explained myself so correctly, will try once more:
I would like to have an extra button on my QAT which will save my document with a filename which has the content of my heading. If this is done and I change the heading afterwards, the original filename should be overwritten with the nem heading content.
I was under the impression this could be done with a macro.
In case you tell me, that is will require a lot of coding, than I suggest to forget it and I will continue with my 'primitive' C&V + C&P methods. The only thing I shall have to take care of is the case where the heading is, on second thought, changed and I will not have two files.
Thanks for your time, I realy do appreciate that.
Stay healty,
Koen
 
 https://files.engineering.com/getfile.aspx?folder=9fa5f656-9532-41ea-aac2-658afdc95890&file=Naamloos.png
Unless you type 'Doc1' in the page header, you either didn't enable macros if the File Save As dialogue shows 'Doc1', or you did something else that causes the macro not to run correctly (e.g. maybe you didn't copy & paste it exactly as it is, into the VBE). Had you enabled macros, saving the document with the macro I supplied in the template (or in the document itself, for that matter) would automatically present the File Save As dialogue with the first paragraph of your heading content as the suggested name. No buttons required. I know it works - I have just re-tested it. Even with a button, nothing will happen unless you enable macros.

Moreover, unless you put whatever macro want to use in the document's template, you will have to add it to every document you require this process to act on - and save that document in the docm format. So, you see, using a template is the best way to go.

Cheers
Paul Edstein
[MS MVP - Word]
 
Paul said:
Unless you type 'Doc1' in the page header, you either didn't enable macros
I did enable macros with the button that shows up un openening
Paul said:
if the File Save As dialogue shows 'Doc1', or you did something else that causes the macro not to run correctly (e.g. maybe you didn't copy & paste it exactly as it is, into the VBE).
I have recopied and saved your macro.

Hmmm ‘funny1’ when I open Macro’s (click on the button Edit) and select the Macro “FileSave” it errors with to modify it errors with ‘Sub of Function is not defined’
Hmmm ‘funny2’when I click on the button ‘Execute’ it errors “Macros are enabled fort his project, please see the online help for ‘host’-application to check how macro’s should be activated.”
Unfortunately my helptext does not give any solution on ‘hostapplication’ or the original word in my Dutch helpfile, which is a non-existing word in the Dutch language, ‘hosttoepassing’.
You keep on telling me, ‘no buttons required’, the macro is executing automaticaly.
Whenever when I try to record/make a macro, the first prompt is Button or Keyboard. This gave me the impression I could assign my macro to a Button.

And if I would put it in Normal.dot?

Maybe the idea afterall is not so good, better keepon Ctrl-V + Ctrl+P and remove any unwanted files by hand.
Thanks for your time, I do appreciate it. Sorry if I am not able to understand. Sorry.

Stay healthy,
Koen
 
when I open Macro’s (click on the button Edit) and select the Macro “FileSave” it errors with to modify it errors with ‘Sub of Function is not defined’
Since the macro uses only standard Word VBA functions, that confirms you have made an error somewhere. I cannot imagine how you could mess this up so badly.
when I click on the button ‘Execute’ it errors “Macros are enabled fort his project, please see the online help for ‘host’-application to check how macro’s should be activated.”
There is no 'Execute' button. Presumably, you mean the 'Run' button. Moreover, what you're saying the error message is makes no sense. Presumably, it actually begins with "Macros are not enabled for this project", which is why the macro won't run - despite what you keep saying, you haven't allowed Word to run macros.

Your previous post also mentioned:
If this is done and I change the heading afterwards, the original filename should be overwritten with the nem heading content.
That is not what you originally asked for and is not what the macro does. Although it saves the file with the new name, it does not overwrite or delete the old one. Extra code would be required for that.

Cheers
Paul Edstein
[MS MVP - Word]
 
Paul,
To correct a few things:
1) the 'execute' is a translation form my Dutch setting, it could just as well be run, cfmd.
2) indeed I missed the 'not' in my message to you, sorry for that.
3) I dont recall telling you I have not allowed Word to run macros, I pressed the button marked with, again translated from Dutch, 'enable content' in the yellow box on starting the document.
4) the last part is indeed added on my first, original post.

Thanks for your time,
I surrender and will continue with Ctrl+V and Ctrl+P
Stay healthy,
Koen
 
Your first post said only:
Would like to create a macro which would save my MSWordfile with a filename which is the same as the Documentheader.
This, the macro does. The post said nothing about deleting or replacing any files.

Your second post added:
Would be nice if the macro replaces the old filename with the new filename
This, the macro also does, by prompting you to save the file with the new name from the header. Hence the old filename is replaced with the new filename. That is not the same as deleting or replacing the old file, however.

As for:
I pressed the button marked with, again translated from Dutch, 'enable content' in the yellow box on starting the document.
There can be two such prompts - one to enable editing and one to enable macros. Either way, it is clear you have not enabled macros.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top