#1
|
|||
|
|||
MS Word Macro
Hi,
I am looking to write a macro for MS Word documents. 1. On Opening a word document, we need to check if the document uses A4 as PageSize. 2. If Yes, then for those pages which are 'Landscape' in orientation, we need to set the Left Margin to 1.8". 3. Forcefully Save. 4. Save must happen before any print command is issued. I will really appreciate for any response. Thanks in advance. Dany |
#2
|
||||
|
||||
It would have helped had you actually posted this in the Word VBA forum. Posting Word VBA questions in the Publisher forum is a fairly sure way of ensuring they got get read by the Word specialists.
Moving thread to the Word VBA forum...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
How about
Code:
Sub FixMargin() Dim oDoc As Document Dim oSection As Section Set oDoc = ActiveDocument If oDoc.Sections(1).PageSetup.PaperSize = wdPaperA4 Then For Each oSection In oDoc.Sections If oSection.PageSetup.Orientation = wdOrientLandscape Then oSection.PageSetup.LeftMargin = InchesToPoints(1.8) End If Next oSection oDoc.Save End If lbl_Exit: Set oDoc = Nothing Set oSection = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
I'm not trying to play "I can write that code in # lines or less" ;-). Just curious and would like comment on advantages/disadvantages (if either) of using defined variables such as oDoc in this case and explicitly setting objects (oSection) to nothing when it appears that oSection is set to nothing automatically anyway.
Code:
Sub FixMarginII() Dim oSection As Section With ActiveDocument If .Sections(1).PageSetup.PaperSize = wdPaperA4 Then For Each oSection In .Sections If oSection.PageSetup.Orientation = wdOrientLandscape Then oSection.PageSetup.LeftMargin = InchesToPoints(1.8) End If Next oSection .Save End If End With lbl_Exit: Exit Sub End Sub |
#5
|
||||
|
||||
I don't think there are any, other than personal programming style.
Both work.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
macro, margins, pagesize |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro for MS Word | ubns | Word VBA | 15 | 04-10-2012 04:37 PM |
Macro in word | khalfenadeem | Word | 1 | 04-11-2011 04:35 PM |
Help with Word macro please | Philcraig69 | Word VBA | 2 | 01-21-2011 04:19 PM |
Word 2003 macro to Word 2007 to 2010 to... | maruapo | Word VBA | 0 | 06-04-2010 03:43 PM |
Word macro | weezypenguin | Word | 0 | 02-25-2010 01:25 PM |