PDA

View Full Version : How to update activeX textbox with a template



QueenofChaos
03-11-2009, 08:17 AM
Hi everyone,
I made a Word template with a Textbox (Control toolbox) in the footer. The goal is to update the value of this Textbox with the path of the file when somebody save his document. For the saving event I found a solution with the event DocumentBeforeSave and the OnTime method but for the update of the Textbox I have some problems.

When I write: ID.Value= Path (ID is my textbox), it doesn't work.

I guess le Textbox of the template is being updated but I want the active document to be updated. I don't know if I make myself clear enought.
Have you any idea how to update the value of the textbox for every document who use this template.

Thanks in advance for your help.

lucas
03-11-2009, 08:37 AM
If the code is in the template and you used:
Private Sub appWord_DocumentBeforeSave _
(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)

Then I think it will only fire if you open and save the template file. Not the cloned document.

You should probably use document new procedure to prompt the user to save the file and then you can put the path in the footer at the same time.........

fumei
03-11-2009, 08:38 AM
Why not just use a Field?

"When I write: ID.Value= Path (ID is my textbox), it doesn't work. "

Post your code. I suspect you do not have the object (the textbox) fully qualified.

QueenofChaos
03-11-2009, 09:50 AM
I use this code to call my subprogram in the class module
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
App.OnTime When:=Now(), Name:="Convert.AfterSaveProcName"
End Sub

and here is the subprogram

Sub AfterSaveProcName()
Dim Path As String
Path = ActiveDocument.FullName
ID.Value = Path
MsgBox "ID a été mis ŕ jour"
End Sub

fumei
03-11-2009, 10:34 AM
And no:

Sub AfterSaveProcName()
Dim Path As String
Path = ActiveDocument.FullName
ID.Value = Path
MsgBox "ID a été mis ŕ jour"
End Sub

is not fully qualified. VBA looks at ID.Value and goes...huh?

ActiveX controls are ONLY known by their name in the ThisDocument module. ONLY in the ThisDocument module. You can easily see this if you go into the ThisDocument module and click the left dropdown (at the top). All your ActiveX controls are listed there, and you can access and use all their events natively from there. If you click the left dropdown in any other module...no ActiveX controls are listed.

Otherwise they must be fully qualified. Your code (Sub AfterSaveProcName) is - or I think it is - NOT in the ThisDocument module. Thus VBA parses ID.Value and goes....huh?

Sub AfterSaveProcName()
Dim Path As String
Path = ActiveDocument.FullName
ThisDocument.ID.Value = Path
MsgBox "ID a été mis ŕ jour"
End Sub



ps. why bother with declaring the variable?

Sub AfterSaveProcName()
ThisDocument.ID.Value = ActiveDocument.FullName
MsgBox "ID a été mis ŕ jour"
End Sub

However, again, I have to ask, why use an ActiveX control (which can have security issues) at all. Why not simply put in a Field?

QueenofChaos
03-11-2009, 11:15 AM
At the beginning the AfterSaveProcName() was in thisDocument but I change. Even with the qualification it doesn't work.
I simply use a activeX because I didn't know that I can use a field and I have no Idea how to use it. If you have good tutorial in mind let me know.

However, Lucas can you explain me how to do, if I want to update the field of the clone file when the user save it. The goal is to replace the template Normal with the custum one.

fumei
03-11-2009, 11:32 AM
"The goal is to replace the template Normal with the custum one."

That is a good goal.

To insert a Field, anywhere, even in the header/footer:

Insert > Field > Filename

As you want the full path, add the \p switch.

Done.

"At the beginning the AfterSaveProcName() was in thisDocument but I change. Even with the qualification it doesn't work."

What do you mean, it doesn't work? Do you get an error? If so...WHAT error? Do you just get nothing happening at all?

Why did you "change it" from ThisDocument?

lucas
03-11-2009, 11:35 AM
Put this in the thisdocument module of the .dot file Change the name of the textbox to match yours in the last line of the code, ie where it says Textbox1

Option Explicit
Private Sub Document_New()
Dim aDialog As Dialog
Dim Maybe As String
Dim CurrentSaveFolder As String

' pick up existing save folder
CurrentSaveFolder = Options _
.DefaultFilePath(Path:=wdDocumentsPath)

' the string for the filename
Maybe = "your filename"

' change the FileOpen folder
' use YOUR folder!!!!!
' ChangeFileOpenDirectory "F:\Temp\Test"

' display FileSaveAs with your filename
' folder is the folder used with ChangeFileOpen
Set aDialog = Application.Dialogs(wdDialogFileSaveAs)
With aDialog
' .Name = Maybe
.Show
End With
ActiveDocument.TextBox1.Value = aDialog.Name
End Sub


I have a sample put together but am not able to post to the forum at this time.

lucas
03-11-2009, 11:37 AM
I'm with Gerry though, a textbox seems unnessesary for what you are trying to do. I'm not a big fan of formfields mostly because I don't have a complete grasp of how they work but it seems like the most logical way to handle your need.

QueenofChaos
03-11-2009, 11:38 AM
I change because I tried everything :( , there is no error but I guess it update in the template texboxe and not in the clone that's the problem.

For the field, I feel so stupid I never thought it could be so easy. The problem is that I want to show the path but in hexadecimal so I have a maccro who converting it. I was reading about how to create a field who call a maccro.

The question is how can I update the field when the user save the document and does it work on the clone file?

lucas
03-11-2009, 11:53 AM
The code I posted will ask them to save the file and will then update the textbox in the cloned document....

fumei
03-11-2009, 12:00 PM
This is the problem when working on code IN the template file itself. You can never be sure if things will work in the cloned document.

No, the field will not automatically update. So if you have the FILENAME field in the template file, a clone will have the field as well. However, it will "Document1", or "Document2".

When you save the clone, it will STAY as "Document1" ( or "Document2", whatever). Even when you open it again, it will still be "Document1".

You have three options.

1. VBA code to explicitly update the field in the header/footer (pseudo-code).
For Each header/footer In Each Section
header/footer.fields.update


2. manually click Print Preview, and then click Close. This will update the field in the header/footer.

3. do #2 via code. Here is the whole thing.
Sub UpdateMe()
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
End Sub
The above could be code in the template file and put on the toolbar as a button, or as a keyboard shortcut. The user click the button (or hits the hotkey), and voila, the field is updated showing the path/filename of the saved clone file.

lucas
03-11-2009, 12:04 PM
I had to type the folder location in manually but it seems to have attached. Unzip this and try it.

lucas
03-11-2009, 12:22 PM
Whoops, you wanted the path, not just the name......hold on. There must be a way.

lucas
03-11-2009, 12:29 PM
This should add the path

fumei
03-11-2009, 12:37 PM
I would like to add my personal rant as usual.

I dislike using ChangeFileOpenDirectory. However, true, sometimes it is appropriate. Except, if you do, I really think one shoud grab and store the current value first, do whatever you are doing, and then...PUT IT BACK to whatever it was.

I think whenever we, as developers, change a user environment and/or environmental values (for essentially our own purposes), we should always make things return back to the way we found it. Unless of course there is an explicit reason for not doing this.

The problem with using ChangeFileOpenDirectory, and NOT putting it back is that users get used to having Folder X be displayed if they click File > Open.

Or, if they are working on another, different, document, then SaveAs should go to where they are used to it going.

ChangeFileOpenDirectory does not just change that folder for the current session. It changes it as a Word environmental value.

Steve you do pick up the value, but you do not put it back.

QueenofChaos: "The problem is that I want to show the path but in hexadecimal so I have a maccro who converting it."

Aaaah. You never mentioned that. Nope, the result of the field will not be in hex. If you want the value in hex, yes you will have to get the string, convert it, and put a hex string in the location.

Here is another alternative: use a blank bookmark in the header/footer.

After you do your conversion to hex, dump it into the bookmark. You can do that even though the bookmark is in the header/footer. I added a keyboard shortcut (Alt-h) for this. The bookmark "HexFileName" is in the header.

' keyboard shortcut = Alt-h
ActiveDocument.Bookmarks("HexFileName").Range.Text = _
"633a5c7a7a7a5c746573745d796164646179616464612e646f63"

fumei
03-11-2009, 12:38 PM
Gotta ask. Why on earth do you want it in hex?

QueenofChaos
03-11-2009, 02:50 PM
I must display the path in Hexo to create an unique ID, it's a request of my client.
Lucas I tried your template but when I opened it nothing happened, I went in debug mode it don't event go into the newdocument event:bug: .
If I understand your code well, it will happen only when you created a new document but it will not happen if you want to rename the file with a save as...
I am really lost, it seems to have a few issues to do this but I have no idea what the best way is and what is the easiest.

Fumei I must admit I don't really understand everything with the bookmark, I must fid out some documentation about this.

fumei
03-12-2009, 09:49 AM
OK, there seems to be some confusion regarding templates.

"I went in debug mode it don't event go into the newdocument event"

The Document_New event executes when you use the template (.DOT) file. This is done by either:

a) File > New and selecting the template file to make a new Document.

b) double clicking the .dot file in Windows Explorer. The default action on a .DOT file is to use it - i,e, clone a new document from it.

Although, yes, you can execute Document_New explicitly.

"but it will not happen if you want to rename the file with a save as..."

When a .DOT file is used to clone a new document, that new document is NOT saved. Therefore you never "rename" it. A document (showing as "Document1", or "Document2") is NOT saved. Clicking Save or SaveAs will BOTH display the SaveAs dialog.

It must be SaveAs...because the file is NOT saved yet. You can not rename it, as it is not named yet.

As for using a bookmark, here is a demo document. Click "Put Into Header" on the top toolbar.

You get an Inputbox asking you to type some text.

Type some text, and then press Enter.

Or, for you mouse people...
1. lift up your hand
2. move your hand over to the mouse
3. move the mouse to put the cursor over "OK"
4. click the left button.

Whatever text you have typed will now show in the header (in the bookmark). If you want to make it "blank" again, just execute the procedure - thus getting the InputBox - and press Enter.

Or, for you mouse people...
1. lift up your hand
2. move your hand over to the mouse
3. move the mouse to put the cursor over "OK"
4. click the left button.

This puts "" - no text - into the bookmark.

The point being is that you can easily put a string into a header. So, get the path/filename, do your conversion to hex, and as this is a string...put that string into the header.

QueenofChaos
03-12-2009, 10:01 AM
I've just solved the problem and Lucas you help me to solve it inderectly. In your sub you show me something I was not sure that it was allow.
To change the TextBox value of the Active document I must call it like this.
ActiveDocument.TexBox.Value ="test"
I feel so dumb because I think about it but, the autocomplete don't display me the TextBox.Value so I was thinking I'm not able to write it.

So now there is no problem, when the user save it update the textbox.

I'd like to thank you for your help and the time you spend for me.:clap: