MS Office Forum / Word / Programming / October 2006
Insert FIELD INCLUDETEXT in Header and Footer Section.
|
|
Thread rating:  |
Jeffery B Paarsa - 05 Oct 2006 16:48 GMT Hello,
I am using insert Field INCLUDETEXT on Headers and Footers sections of a few Word Template “*.dot” that will be stored on a variable drive with variable directory names due to security reason. When I use {INCLUDETEXT “C:\\ZZZ\\YYY.doc” FootAdd \*charformat} I have no problem, but due to requirement of having different drive letter and different directory name I tried to use different combination of the following instead. {INCLUDETEXT FileSystem.CurDir “\\YYY.doc” FootAdd \*charformat}. No luck so far and I am getting "Error! Not a valid filename". Any idea?
 Signature Jeff B Paarsa
Jonathan West - 05 Oct 2006 17:07 GMT > Hello, > [quoted text clipped - 10 lines] > am > getting "Error! Not a valid filename". Any idea? Fields don't run VBA commands.
If you want to do something like this, you will need to create an AutoNew macro that builds the string you need for the full pathname of the target file (including the opening and closing quotes and the doubled backslashes), and then inserts an INCLUDETEXT field using your file path into the header of the newly created document.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jeffery B Paarsa - 05 Oct 2006 22:23 GMT Hello,
Okay in the AutoNew macro I am building the string text that puts together the drive letter/pathname/filename and necessary bookmark name and etc like {“G:\\MRMDInc\\SFooter.doc” FootAdd \*charformat}. Could you please elaborate on “> and then inserts an INCLUDETEXT field using your file path into the header of the newly created document.” How? Do I need to put the built string into the newly created document footer Template .dot or actual document .doc? I don’t know how and I am completely lost on that. Your direction is really appreciated in advance.
 Signature Jeff B Paarsa
> > Hello, > > [quoted text clipped - 18 lines] > and then inserts an INCLUDETEXT field using your file path into the header > of the newly created document. Jonathan West - 06 Oct 2006 00:10 GMT > Hello, > [quoted text clipped - 9 lines] > document .doc? I don't know how and I am completely lost on that. Your > direction is really appreciated in advance. Use the Fields.Add method to add a field of the appropriate type. Set the range parameter to where you want to insert the field, the Type parameter to wdFieldEmpty, and the Code parameter to the complete field code.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup
Lüko Willms - 07 Oct 2006 07:14 GMT Am Thu, 5 Oct 2006 15:48:02 UTC, schrieb Jeffery B Paarsa <JeffBPaarsa@Yahoo.com> auf microsoft.public.word.vba.general :
> I am using insert Field INCLUDETEXT on Headers and Footers sections of a few > Word Template *.dot that will be stored on a variable drive with variable > directory names due to security reason. When I use {INCLUDETEXT > C:\\ZZZ\\YYY.doc FootAdd \*charformat} I have no problem, Is the directory source of the INCLUDETEXT always in the same relation to the current directory of the document or its template? Couldn't you then use a relative path name instead of an absolut path name?
Yours, L.W.
Jeffery B Paarsa - 18 Oct 2006 21:46 GMT > I am using insert Field INCLUDETEXT on Headers and Footers sections of a few > Word Template *.dot that will be stored on a variable drive with variable > directory names due to security reason. When I use {INCLUDETEXT > C:\\ZZZ\\YYY.doc FootAdd \*charformat} I have no problem, Is the directory source of the INCLUDETEXT always in the same relation to the current directory of the document or its template? Couldn't you then use a relative path name instead of an absolut path name?
Yours, L.W.
Yes the directory source of the INCLUDETEXT is always the same as the directory of word template itself. I am putting INCLUDETEXT "C:\\ZZZ\\YYY.doc" FootAdd \*charformat in the inserted field on the template, and I use the following code to update the field. Update works fine provided that YYY.doc exist on ZZZ directory on C drive. I run the following code inside of AutoNew or inside of the code itself.
Dim oField As Field Dim oSection As Section Dim oHeader As HeaderFooter Dim oFooter As HeaderFooter For Each oSection In ActiveDocument.Sections For Each oHeader In oSection.Headers If oHeader.Exists Then For Each oField In oHeader.Range.Fields oField.Update Next oField End If Next oHeader For Each oFooter In oSection.Footers If oFooter.Exists Then For Each oField In oFooter.Range.Fields oField.Update Next oField End If Next oFooter Next oSection
Problem is that Word tries to run the INCLUDETEXT "C:\\ZZZ\\YYY.doc" FootAdd \*charformat at the run time. If directory & source doc is there it updates it otherwise keep whatever it was defaulted at the time of insert field or filed definition. I need to be able to update this "C:\\ZZZ\\YYY.doc" FootAdd \*charformat at the run time prior to creating a new document from the template. I have resolved the problem of creating a new document form a predefined template by using: Dim ProbList As Document Set ProbList = Word.Documents.Add(Template:=Application.MacroContainer.Path + "\(XX)ProbList.dot", Visible:=False)
But so far I have not been able to remove the dependency of the Header/Footer field on drive/directory names.
Thanks for your help.
 Signature Jeff B Paarsa
> Am Thu, 5 Oct 2006 15:48:02 UTC, schrieb Jeffery B Paarsa > <JeffBPaarsa@Yahoo.com> auf microsoft.public.word.vba.general : [quoted text clipped - 11 lines] > Yours, > L.W. Lüko Willms - 19 Oct 2006 22:44 GMT Am Wed, 18 Oct 2006 20:46:02 UTC, schrieb Jeffery B Paarsa <JeffBPaarsa@Yahoo.com> auf microsoft.public.word.vba.general :
> Yes the directory source of the INCLUDETEXT is always the same as the > directory of word template itself. then you could use ActiveDocument.AttachedTemplate.Path instead of a hardcoded path and file name for your text file to be included, but you would probably have to insert the INCLUDETEXT-fields by your event-procedure for Document_New, maybe at predefined bookmarks, or update them.
BTW, instead of looping thru all the Fields to be updated, you could use a global "ActiveDocument.Fields.Update" which acts on all fields in the document. Being lazy, I do that...
Yours, L.W.
Jeffery B Paarsa - 19 Oct 2006 23:32 GMT > but you would probably have to insert the INCLUDETEXT-fields by your > event-procedure for Document_New, maybe at predefined bookmarks, or > update them. That is exactly what I don't know how... I tried multiple way to update my INCLUDETEXT field using the differnt methods Field.Add but I could not get it to work. I search the board and WMPV sites for solution and I could not find anything close to my problem. Do you know how to do this Fields.ADD to the Footer? Do you have a sample code please? Thanks for the response though.
Regards
 Signature Jeff B Paarsa
> Am Wed, 18 Oct 2006 20:46:02 UTC, schrieb Jeffery B Paarsa > <JeffBPaarsa@Yahoo.com> auf microsoft.public.word.vba.general : [quoted text clipped - 15 lines] > Yours, > L.W. Lüko Willms - 20 Oct 2006 07:58 GMT Am Thu, 19 Oct 2006 22:32:02 UTC, schrieb Jeffery B Paarsa <JeffBPaarsa@Yahoo.com> auf microsoft.public.word.vba.general :
> Do you know how to do this Fields.ADD to the > Footer? Do you have a sample code please? Here is a sample from a program which creates a new document and then inserts fields in header and footer, namely {TILE} in the header and '{PAGE} of {NUMPAGES}' in the footer:
<CODE> '::::::::::::::::::::::::: With newDoc.Sections(1) .Headers(wdHeaderFooterPrimary).Range.Fields.Add _ Range:=.Headers(wdHeaderFooterPrimary).Range, _ Type:=wdFieldTitle .Headers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment _ = wdAlignParagraphCenter ' .Footers(wdHeaderFooterPrimary).Range.Fields.Add _ Range:=.Footers(wdHeaderFooterPrimary).Range, _ Type:=wdFieldPage .Footers(wdHeaderFooterPrimary).Range.Collapse _ Direction:=wdCollapseEnd .Footers(wdHeaderFooterPrimary).Range.InsertAfter " von " ' here I have to redefine a range, so that the next field is ' inserted after the text and does not replace it: Set myRange = .Footers(wdHeaderFooterPrimary).Range myRange.Collapse Direction:=wdCollapseEnd .Footers(wdHeaderFooterPrimary).Range.Fields.Add _ Range:=myRange, Type:=wdFieldNumPages .Footers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment _ = wdAlignParagraphCenter End With '::::::::::::::::::::::::: </CODE>
you would have to code a
.Headers(wdHeaderFooterPrimary).Range.Fields.Add _ Range:=MyRange, Type:=wdFieldIncludeText, _ Text:=.AttachedTemplate.Path _ & Application.PathSeparator & "textfile.txt", _ True
Well, maybe not "wdHeaderFooterPrimary", since I understand that you have various headers and footers. The "True" at the and creates this "\* MERGEFORMAT" to preserve the formatting given to the field resp. the range it is replacing or inserted into.
Please note that I am also still a beginner and the above could maybe be coded in a better way -- but I for myself just don't know better. Always learning.
My example above does not use a template, but creates everything from scratch; you start with a template which just -- maybe -- needs some editing.
I think you could also set the ShowFieldCodes of the View on your HeaderFooter-Object to True and then change the filename in the INCLUDETEXT field by a Search/Replace.
At the end, don't forget the .Fields.Update
Yours, L.W.
Jeffery B Paarsa - 24 Oct 2006 19:07 GMT Hi,
I am using VBA for my coding... Some how I had to play with your suggested code and turn it into: ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Fields.Add _ Range:=Rng, Type:=wdFieldIncludeText, _ Text:=Application.MacroContainer.Path _ & Application.PathSeparator & "$Location.doc", True
Still would not accept the "True"... I removed it get it and got it compiled but gave me run time error of 4605 "This command is not available". I tried to get it compiled as close to your suggested code as I could... some how it would not get compiled.
Any how I really thank your effort and suggestions.
 Signature Jeff B Paarsa
> Am Thu, 19 Oct 2006 22:32:02 UTC, schrieb Jeffery B Paarsa > <JeffBPaarsa@Yahoo.com> auf microsoft.public.word.vba.general : [quoted text clipped - 65 lines] > Yours, > L.W. Lüko Willms - 24 Oct 2006 22:56 GMT Am Tue, 24 Oct 2006 18:07:01 UTC, schrieb Jeffery B Paarsa <JeffBPaarsa@Yahoo.com> auf microsoft.public.word.vba.general :
> Still would not accept the "True"... I removed it get it and got it compiled > but gave me run time error of 4605 "This command is not available". Looking at my code, I recognize that I did not use the "True" parameter, only in the untested suggestion I made.
My code was developed unter Office 2000 and worked also under Office 2003.
Yours, L.W.
|
|
|