Solved

How to autoformat word documents with given parameters.

Posted on 2004-10-17
14
218 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
ID: 12333147
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
ID: 12358643
I want help on Line by line and Para by Para formating. Canb u help please.
0
 
LVL 9

Expert Comment

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

Expert Comment

by:Shahid Thaika
ID: 12360948
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
ID: 12368487
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
ID: 12372193
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:micazone
ID: 12378713
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
ID: 12379266
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
ID: 12380108
All pages
0
 

Author Comment

by:micazone
ID: 12493073
Waiting for your reply.
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12493532
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
ID: 12761401
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 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

25 Experts available now in Live!

Get 1:1 Help Now