Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

How to autoformat word documents with given parameters.

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
micazone
Asked:
micazone
  • 7
  • 5
1 Solution
 
Shahid ThaikaCommented:
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
 
micazoneAuthor Commented:
I want help on Line by line and Para by Para formating. Canb u help please.
0
 
Shahid ThaikaCommented:
I will give a try and let you know :)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Shahid ThaikaCommented:
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
 
micazoneAuthor Commented:
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
 
Shahid ThaikaCommented:
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
 
micazoneAuthor Commented:
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
 
Shahid ThaikaCommented:
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
 
micazoneAuthor Commented:
All pages
0
 
micazoneAuthor Commented:
Waiting for your reply.
0
 
Shahid ThaikaCommented:
Oops sorry I forgot this thread. Lots of things had come up. I'll go through it once again :).
0
 
Shahid ThaikaCommented:
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

Industry Leaders: 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!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now