Choose margins at startup

S

Sjaakve

Hi,

Is it possible to write a macro that does this:

Run at word 2007 startup,

Check font of first word,

Change margins if font is Courier New

If not, do nothing (and continue to open document) .


I tried recording a macro to find out how to change te margin end
ended up with this:

Sub NMargin()

With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.7)
.RightMargin = CentimetersToPoints(1.7)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With
End Sub
 
G

Graham Mayor

Sjaakve said:
Hi,

Is it possible to write a macro that does this:
Yes!

Run at word 2007 startup,

If you want it to do the rest of the items in your message, then it should
run when you open a document and not when you start Word.
Check font of first word,
Change margins if font is Courier New

If not, do nothing (and continue to open document) .

What is the purpose of the exercise? There may be better ways of achieving
this..
I tried recording a macro to find out how to change the margin end
ended up with this:

This will do what you ask

Sub AutoOpen()
If ActiveDocument.Words(1).Font.name = "Courier New" Then
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.7)
.RightMargin = CentimetersToPoints(1.7)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With
End If
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sjaakve

Hi,

You asked what the purpose of the macro was.

I use a 3d moddeling programm to draw steel structures. The programm
is able to make lists of used materials, bolts etc. The lists are
exported as .doc but have a wrong layout and something is wrong with
the conversion. The font is always Courier New. The layout problem is
fixable but takes a lot of work. The present solution is opening the
list, manuale changing the margins, saving as 2003 document. This
fixes the problem, but is also very time-consuming.

I also came up with a code:

-----------------------------------------------------------------------------------------------------------

Sub NMargin()
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.27)
.RightMargin = CentimetersToPoints(1.27)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With

ActiveDocument.SaveAs FileName:=ActiveDocument.FullName, _
FileFormat:=wdFormatDocument, LockComments:=False,
Password:="", _
AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False,
SaveFormsData _
:=False, SaveAsAOCELetter:=False

End Sub

-----------------------------------------------------------------------------------------------------------

I've attached te macro to a bottum in the quick-acces bar. It changes
the margins and saves the document in de same folder under the same
name. If incorporated in your code you'd get this.

-----------------------------------------------------------------------------------------------------------

Sub AutoOpen()
If ActiveDocument.Words(1).Font.name = "Courier New" Then
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.7)
.RightMargin = CentimetersToPoints(1.7)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With

ActiveDocument.SaveAs FileName:=ActiveDocument.FullName, _
FileFormat:=wdFormatDocument, LockComments:=False,
Password:="", _
AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False,
SaveFormsData _
:=False, SaveAsAOCELetter:=False

End If

End Sub

-----------------------------------------------------------------------------------------------------------

I'll do some tests and report back.

Thanks,

Sjaak
 
S

Sjaakve

Hi,

You asked what the purpose of the macro was.

I use a 3d moddeling programm to draw steel structures. The programm
is able to make lists of used materials, bolts etc. The lists are
exported as .doc but have a wrong layout and something is wrong with
the conversion. The font is always Courier New. The layout problem is
fixable but takes a lot of work. The present solution is opening the
list, manuale changing the margins, saving as 2003 document. This
fixes the problem, but is also very time-consuming.

I also came up with a code:

-----------------------------------------------------------------------------------------------------------

Sub NMargin()
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.27)
.RightMargin = CentimetersToPoints(1.27)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With

ActiveDocument.SaveAs FileName:=ActiveDocument.FullName, _
FileFormat:=wdFormatDocument, LockComments:=False,
Password:="", _
AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False,
SaveFormsData _
:=False, SaveAsAOCELetter:=False

End Sub

-----------------------------------------------------------------------------------------------------------

I've attached te macro to a bottum in the quick-acces bar. It changes
the margins and saves the document in de same folder under the same
name. If incorporated in your code you'd get this.

-----------------------------------------------------------------------------------------------------------

Sub AutoOpen()
If ActiveDocument.Words(1).Font.name = "Courier New" Then
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.7)
.RightMargin = CentimetersToPoints(1.7)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With

ActiveDocument.SaveAs FileName:=ActiveDocument.FullName, _
FileFormat:=wdFormatDocument, LockComments:=False,
Password:="", _
AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False,
SaveFormsData _
:=False, SaveAsAOCELetter:=False

End If

End Sub

-----------------------------------------------------------------------------------------------------------

I'll do some tests and report back. It might be nessecary to reprogram
the If. It try and find some other specific of the list.

Thanks,

Sjaak
 
G

Graham Mayor

In the circumstances the macro is probably the simplest option :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sjaakve

Hi,

it is indeed neccasary to add more variables to the If.

I've noticed the first and last line are all made up of dashes (-).

How can i put this in the If?

Sjaak
 
G

Graham Mayor

If you are going to run the macro from a toolbar button, you don't need the
condition?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sjaakve

I only linked the macro to a botton because i didn't how to
automatically run the macro. If the additional condition can be
included in the code, the macro has achieved its purpose.

I tried to fiend some code:

If ActiveDocument.Characters(1) = "-"

this only add the first character to the condition, not the first line
(and the last line)

It it possible to add the first and last line to the condition.
 
G

Graham Mayor

I don't understand what it is you want to do about the hyphens.

The following demonstrates how to test for a hyphen at the first character.

If ActiveDocument.Characters(1).Text = "-" Then
MsgBox "true"
Else
MsgBox "False"
End If

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sjaakve

correct, it tests if the first characted is a hyphen. Can I also test
if the first 5 characters, and the last 5 characters of the document
are hyphens?

The change of another document fitting this condition is almost zero.
This way i can insure the macro will only auto-execute when a list is
opened. not if another document is opened.

Summarizing, can I put the following in the condition for the macro:

first 5 characters of document are hyphens
last 5 characters of document are hyphens
document font is courier new (already in condition)


---------------------------------
Sub AutoOpen()
If CONDITIONS Then
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.7)
.RightMargin = CentimetersToPoints(1.7)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With

ActiveDocument.SaveAs FileName:=ActiveDocument.FullName, _
FileFormat:=wdFormatDocument, LockComments:=False,
Password:="", _
AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False,
SaveFormsData _
:=False, SaveAsAOCELetter:=False

End If

End Sub
------------------------------------
 
G

Graham Mayor

If the first and last five characters (the last character is actually a
paragraph mark) are hyphens and the first paragraph (the hyphens) is
formatted in Courier New font, then the following will work.

Sub AutoOpen()
Dim bSChars As Long
Dim bEChars As Long
Dim i As Long
Dim j As Long
bSChars = 0
bEChars = 0
With ActiveDocument
If .Characters.Count > 6 Then
For i = 1 To 5
If .Characters(i).Text = "-" Then
bSChars = bSChars + 1
End If
Next i
For j = .Characters.Count To .Characters.Count - 6 Step -1
If .Characters(j).Text = "-" Then
bEChars = bEChars + 1
End If
Next j
'MsgBox bSChars & " " & bEChars
If .Paragraphs(1).Range.Font.name = "Courier New" _
And bSChars = 5 And bEChars = 5 Then
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.7)
.RightMargin = CentimetersToPoints(1.7)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With
End If
.Save
End If
End With
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sjaakve

Thanx,

Look good. I'll test it tonight.

one more question. What does the .Save command do?

The command i had were recorded from word and slightly edited by me.
It saved the document in the same folder under the same name but in
2003 format. Does .Save do the same?

Sjaak
 
G

Graham Mayor

ActiveDocument.Save simply saves the document in its original format but
with the changes applied by the macro. I had assumed from the thread that
the document came to you as a Word document? If you need to saveas to change
the document format then

replace .Save
with
..SaveAs FileName:=ActiveDocument.FullName, _
FileFormat:=wdFormatDocument

Note that this will retain the same file extension as the original document.
It you want to change the filename extension you will need some extra code.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sjaakve

Hi Graham,

I've inserted the macro as you wrote. I did not work at first. I
deleted the ' before the MsgBox to check if both variable were acually
5. bEChars was 6. I changed the count van -6 to -5 and it worked.

I've added some code that addes the date the list was changed and by
who. Now, if the same list is opened again, the macro won't run. Also,
now i can see who processed the list.

I've posted it hereunder.

-----------------------------------
Sub AutoOpen()
Dim bSChars As Long
Dim bEChars As Long
Dim i As Long
Dim j As Long
bSChars = 0
bEChars = 0
With ActiveDocument
If .Characters.Count > 6 Then
For i = 1 To 5
If .Characters(i).Text = "-" Then
bSChars = bSChars + 1
End If
Next i
For j = .Characters.Count To .Characters.Count - 5 Step -1
If .Characters(j).Text = "-" Then
bEChars = bEChars + 1
End If
Next j
'MsgBox bSChars & " = " & bEChars
If .Paragraphs(1).Range.Font.Name = "Courier New" _
And bSChars = 5 And bEChars = 5 Then
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1.27)
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.7)
.RightMargin = CentimetersToPoints(1.7)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(1.27)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With

'make text black, not bold and don't show writing signs
Selection.WholeStory
Selection.Font.Color = wdColorAutomatic
Selection.Font.Bold = False
ActiveWindow.ActivePane.View.ShowAll = False

'add some text in white so macro won't run again if list was
changed in the past
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeText Text:="processed on "
Selection.TypeText Format(Now(), "mm/dd/yyyy")
Selection.TypeText Text:=" by "
Selection.TypeText Application.UserName
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Font.Color = -603914241
Selection.Font.Name = "Arial"
Selection.Font.Size = 10
Selection.HomeKey Unit:=wdStory

'Save text in same folder under same name as 2003 file
.SaveAs FileName:=ActiveDocument.FullName, _
FileFormat:=wdFormatDocument

End If

End If
End With
End Sub
--------------------------------------------------
 

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