Solved

How to autoformat word documents with given parameters.

Posted on 2004-10-17
14
227 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
Independent Software Vendors: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using Access 8 84
Prevent checkbox click event occur while editing it in vb6 8 65
Added a column screws up code 5 69
add criteria to query in VB, Access 2003 2 31
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

749 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