Solved

How to autoformat word documents with given parameters.

Posted on 2004-10-17
14
215 Views
Last Modified: 2008-02-01
Is there any way to  batch auto format word documents with Pagesetup, Paragraph formatting, Line formatting or even word by word formating through VB script.
Shyam
0
Comment
Question by:micazone
  • 7
  • 5
14 Comments
 
LVL 9

Expert Comment

by:Shahid Thaika
Comment Utility
You can automate them... But how you want it, that depends on you.





Private WApp As New Word.Application
Private WDoc As Word.Document

Private Sub Form_Load()
Set WDoc = WApp.Documents.Open("D:\GMAT Documents\apogee.doc")
WDoc.PageSetup.TopMargin = 72 * 1.25 '72 * [Inches]
WDoc.PageSetup.LeftMargin = 72 * 1 '72 * [Inches]
WDoc.PageSetup.RightMargin = 72 * 1 '72 * [Inches]
WDoc.PageSetup.BottomMargin = 72 * 1 '72 * [Inches]

WDoc.Paragraphs.Alignment = wdAlignParagraphJustify 'Many setting available

WDoc.Paragraphs.LineSpacing = 18

WDoc.Save
WDoc.Close
WApp.Quit

Set WDoc = Nothing
Set WApp = Nothing
End Sub



Hope this helps :)
0
 

Author Comment

by:micazone
Comment Utility
I want help on Line by line and Para by Para formating. Canb u help please.
0
 
LVL 9

Expert Comment

by:Shahid Thaika
Comment Utility
I will give a try and let you know :)
0
 
LVL 9

Expert Comment

by:Shahid Thaika
Comment Utility
Ok here is the full code. :)


-------------------------------------------------------------------------------------------------------------
Private WApp As New Word.Application
Private WDoc As Word.Document

Private Sub Form_Load()
Set WDoc = WApp.Documents.Open("D:\GMAT Documents\apogee.doc")
WDoc.PageSetup.TopMargin = 72 * 1.25 '72 * [Inches]
WDoc.PageSetup.LeftMargin = 72 * 1 '72 * [Inches]
WDoc.PageSetup.RightMargin = 72 * 1 '72 * [Inches]
WDoc.PageSetup.BottomMargin = 72 * 1 '72 * [Inches]

WDoc.Paragraphs.Alignment = wdAlignParagraphJustify 'Many setting available

'Paragraphwise formatting.
WDoc.Paragraphs(1).Alignment = wdAlignParagraphLeft
WDoc.Paragraphs(2).Alignment = wdAlignParagraphRight
WDoc.Paragraphs(3).Alignment = wdAlignParagraphJustify
WDoc.Paragraphs(4).Alignment = wdAlignParagraphCenter

'Line spacing for each paragraph
WDoc.Paragraphs(1).LineSpacing = 14

WDoc.Paragraphs(3).LineSpacing = 24

WDoc.Paragraphs(5).LineSpacing = 18

'To make para 3 bold
WDoc.Paragraphs(3).Range.Bold = True

'To format sentences one, two and three in para 5
WDoc.Paragraphs(5).Range.Sentences(1).Bold = True
WDoc.Paragraphs(5).Range.Sentences(2).Italic = True
WDoc.Paragraphs(5).Range.Sentences(3).Bold = True
WDoc.Paragraphs(5).Range.Sentences(3).Italic = True


WDoc.Save
WDoc.Close
WApp.Quit

Set WDoc = Nothing
Set WApp = Nothing

Unload Me
End Sub
0
 

Author Comment

by:micazone
Comment Utility
Nice script, but very simple.
Can we make it more technical, the following way.
1. If Paragraphs has only Single Line;
    -Define Style as Heading1
   - Font formatiing
   - Indent and alignment
   - Line and Para spacing
   - replace exisiting Tab position with new value
   Else
   - Define Style as Body text
    - Font formatiing
   - Indent and alignment
   - Line and Para spacing
   - replace exisiting Tab position with new value
   End If
AND SHOULD SKIP NUMBERED LISTS AND TABLES.
 2. If there is any Numbered List it should SKIP 1 and format numbered list with given parameters
    as Font, size, indent, Tab Position.
3. If There is any Table it should skip both 1 & 2 and format table with given parameters like cell height, width, font, row height etc.
Thanks
0
 
LVL 9

Accepted Solution

by:
Shahid Thaika earned 500 total points
Comment Utility
Hi micazone, I could manage most of the things that you wanted. Besides, this is the first time I am trying whatever things you wanted. I got them all by trial and error :).



---------------------------------------------------------------------------------------------------------------
Private WApp As New Word.Application
Private WDoc As Word.Document

Private Sub Form_Load()
Set WDoc = WApp.Documents.Open("D:\temp.doc")

GoTo XYZ:

WDoc.PageSetup.TopMargin = 72 * 1.25 '72 * [Inches]
WDoc.PageSetup.LeftMargin = 72 * 1 '72 * [Inches]
WDoc.PageSetup.RightMargin = 72 * 1 '72 * [Inches]
WDoc.PageSetup.BottomMargin = 72 * 1 '72 * [Inches]

WDoc.Paragraphs.Alignment = wdAlignParagraphJustify 'Many setting available

'Paragraphwise formatting.
WDoc.Paragraphs(1).Alignment = wdAlignParagraphLeft
WDoc.Paragraphs(2).Alignment = wdAlignParagraphRight
WDoc.Paragraphs(3).Alignment = wdAlignParagraphJustify
WDoc.Paragraphs(4).Alignment = wdAlignParagraphCenter

'Line spacing for each paragraph
WDoc.Paragraphs(1).LineSpacing = 14

WDoc.Paragraphs(3).LineSpacing = 24

WDoc.Paragraphs(5).LineSpacing = 18

'To make para 3 bold
WDoc.Paragraphs(3).Range.Bold = True

'To format sentences one, two and three in para 5
WDoc.Paragraphs(5).Range.Sentences(1).Bold = True
WDoc.Paragraphs(5).Range.Sentences(2).Italic = True
WDoc.Paragraphs(5).Range.Sentences(3).Bold = True
WDoc.Paragraphs(5).Range.Sentences(3).Italic = True




'heading style
Dim Cnt As Integer
For Cnt = 1 To WDoc.Paragraphs.Count
    With WDoc.Paragraphs(Cnt)
        If .Range.Sentences.Count = 1 Then
            .Range.Style = "Heading 1"
            .Range.Font.Size = 24
            .Range.Font.Bold = True
            .Range.Font.Name = "Arial Black"
            .LineSpacing = 24
            .Space2 'Not sure what this does
            .Indent
        Else
            .Range.Style = "Body Text"
            .Range.Font.Size = 18
            .Range.Font.Bold = False
            .Range.Font.Name = "Arial"
            .LineSpacing = 18
            .Indent
            .Indent
        End If
    End With
Next Cnt

XYZ:
For Cnt = 1 To WDoc.Tables.Count
    With WDoc.Tables(Cnt)
        .Rows.Height = 36
        .Columns.Width = 100
    End With
Next Cnt

WDoc.Save
WDoc.Close
WApp.Quit

Set WDoc = Nothing
Set WApp = Nothing

Unload Me
End Sub




Hope this info helps
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:micazone
Comment Utility
Thanks. It works partially correct.
The problem:
1. It does not format Single Line Para of next page at the starting of page.
2. It clears all formattings of other paragraphs.
0
 
LVL 9

Expert Comment

by:Shahid Thaika
Comment Utility
Hmm... I'll try to look into it. Does it clear formatting for the paragraphs of the other pages or the same page as well?
0
 

Author Comment

by:micazone
Comment Utility
All pages
0
 

Author Comment

by:micazone
Comment Utility
Waiting for your reply.
0
 
LVL 9

Expert Comment

by:Shahid Thaika
Comment Utility
Oops sorry I forgot this thread. Lots of things had come up. I'll go through it once again :).
0
 
LVL 9

Expert Comment

by:Shahid Thaika
Comment Utility
I think I can answer that question. I didn't exactly answer his question completely. Though I did answer the basics required, the final solution is still pending. Sorry mica, had lots of work hitting me in the past few weeks :(
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now