Macro location changing results re Header/Footer

C

Christine

I am finding that where a macro is located changes the results of
running the macro. I am currently trying to create a stand alone Word
document with a few simple controls that will generate another Word
document with certain information and a header/footer. I am finding
out that if I put a SetupHdrFtr routine in the ThisDocument module
then it will not work properly, but if it is in a module in Normal
then it works fine. For example, the following routine creates a new
document and puts a header/footer in it and one line of text in the
body of the document. At least that's what happens if the routine is
in a vba module in Normal. But it does not work in the ThisDocument
vba module of my Conrol.doc.

Does anyone have any idea how to make it work in ThisDocument vba
module of my Control.doc? I really want to keep it simple for the
customer who will use it and not have them need to be messing with
placing any extraneous macro documents anyplace else. I want to be
able to just email them a single Word doc and have them be able to use
it right off the bat.

Any ideas would be much appreciated. Thanks, Christine

Sub NewFileWithHdrFtr()
Documents.Add DocumentType:=wdNewBlankDocument
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or
ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="this is the text for the header"
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.TypeText Text:=vbTab & vbTab
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.TypeText "this should be in body"
End Sub
 
D

Doug Robbins - Word MVP

I am not sure why you are not creating a template for the customer to save
into their user or workgroups template folder and then use by selecting it
after File>New.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Christine

Doug,
Thanks for responding. I don't think a template would be helpful for
my problem. The Word document that I am calling my Control document
actually encapsulates a process to iterate through a folder of files
extracting information from files in a folder and creating an output
Word doc with the extracted information. The control document has some
controls (command buttons,text boxes etc ) as a User interface to
select the folder to read the files from and determine which
information to extract etc. It also contains a sort of Help file via
hyperlinks and bookmarks within the Control document and all the code
to support the interface and the processing is in the THisDocument vba
module of the Control document. It creates the output doc and I wanted
to create and initialize the Header/Ftr of the output document. The
exact contents of the Header/Footer would vary with each run. So I
don't see how a template would help for either the Control Document or
the Output document.

Hope this explanation helps you to understand better what I am trying
to do. As I mentioned before, I want a single "stand-alone" solution,
which does not require any kind of User installation, and do not want
to rely upon routines in any external modules. Do you know why code
that works in an external module does not work in ThisDocument? Any
workarounds?

Thanks for your help.
Christine
 
D

Doug Robbins - Word MVP

Hi Christine,

I don't think any of that changes my opinion that your Control document
should be a template with the command buttons, text boxes etc on a userform
in that template.

While this

Dim myrange As range
With ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).range.Text = "This is the text for the
header"
.Footers(wdHeaderFooterPrimary).range.Text = vbTab & vbTab & "Page "
Set myrange = .Footers(wdHeaderFooterPrimary).range
myrange.Collapse wdCollapseEnd
myrange.Fields.Add myrange, wdFieldPage
End With

is a better way of doing the same as

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or
ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="this is the text for the header"
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.TypeText Text:=vbTab & vbTab
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage

I would have a {DOCVARIABLE "varheadertext" } field in the header of the
template, have the Page { PAGE } field set up in the footer of the template
and have the following commands in your code to insert the text into the
header

ActiveDocument.Variables("varheadertext").Value = "This is the text for the
header"
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields.Updat
e

two lines of code to replace 14
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top