Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-17-2012, 10:02 AM
dany dany is offline MS Word Macro Windows XP MS Word Macro Office 2007
Novice
MS Word Macro
 
Join Date: May 2012
Posts: 1
dany is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 06-16-2015, 09:39 PM
macropod's Avatar
macropod macropod is offline MS Word Macro Windows 7 64bit MS Word Macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 06-17-2015, 12:41 AM
gmayor's Avatar
gmayor gmayor is offline MS Word Macro Windows 7 64bit MS Word Macro Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #4  
Old 06-17-2015, 04:22 AM
gmaxey gmaxey is offline MS Word Macro Windows 7 32bit MS Word Macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 06-17-2015, 06:44 AM
gmayor's Avatar
gmayor gmayor is offline MS Word Macro Windows 7 64bit MS Word Macro Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
Reply

Tags
macro, margins, pagesize



Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Word Macro 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
MS Word Macro 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft