Solved

How to autoformat word documents with given parameters.

Posted on 2004-10-17
14
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

734 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