View Single Post
 
Old 03-17-2016, 11:44 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,994
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

As Paul suggests, your code could be massively simpler if you knew what template was being used for your Document.Add but ignoring that, you should be getting away from ActiveDocument and Selection objects completely.

Once you have done the following line, you should not touch ActiveDocument again
Set doc = .Documents.Add

eg.
Code:
With objWord
  .Visible = True
  Set doc = .Documents.Add
  doc.SaveAs CurrentProject.Path & "\TestDoc.doc"
End With

'Title/IntroPage
With doc.Paragraphs(1).Range
  .ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
  .ParagraphFormat.SpaceAfter = 0
  .ParagraphFormat.SpaceBefore = 0
  .InsertAfter vbCr
End With

Dim aTbl as Word.Table
'Insert Gray Table
Set aTbl = doc.Tables.Add Range:=doc.Paragraphs(2).Range, NumRows:=1, NumColumns:=1, _ DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed

With aTbl
  .Style = "Table Grid"
  .ApplyStyleHeadingRows = True
  .ApplyStyleLastRow = False
  .ApplyStyleFirstColumn = True
  .ApplyStyleLastColumn = False
  .ApplyStyleRowBands = True
  .ApplyStyleColumnBands = False
End With
Note that you are making assumptions because you don't know the template nor the colour palette. So the above code may not return a gray table on other machines since you don't know what style definitions or default colour palettes are applied.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 03-17-2016 at 11:45 PM. Reason: Paul beat me to it
Reply With Quote