View Single Post
 
Old 03-17-2016, 03:01 PM
rpb925 rpb925 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Mar 2016
Location: Sydney
Posts: 17
rpb925 is on a distinguished road
Default Adding tables to Created word document whilst other word document open Help

Hi all. I'm stumped but I think there is a simple solution out there. Any help would be appreciated.

I have an access database which I pull data out of and put in word documents. So far so good.

It all works without tables in it if there are many word documents open.

I have been requested to place in tables in the document.

Also the script I have with tables works if no other word documents are open.

PROBLEM
With tables and other word documents open it goes to word document focus is when it reaches location where tables are added. That is script opens new word document > insert texts > gets to add table section > goes to a previously opened document where focus was > insert tables > I get frustrated

I have spent 5 hours trying different things and come to the conclusion that I have no idea what I am doing. I think it has something to do with either the words " Active document" or "Selection" in the vba. I have added my vba below. The first section is creating recordsets so no issues there. The area of concern is green but everything purple is word related.

I need to add the tables to the newly created word document using the script whilst other documents are open.

Private Sub but_Unsatisfactory_Click()

Dim objWord As Word.Application
Dim doc As Word.Document
Dim WordHeaderFooter As HeaderFooter
Dim QueryA As DAO.Recordset
Dim QueryB As DAO.Recordset
Dim dbs As DAO.Database
Dim strSQLA As String
Dim strSQLB As String
Dim myrange As Range
Dim DA As String
Dim DAX As String
Dim Variable As String
Dim Trange As Range
If Forms!frm_Assess!lst_Referrals.Column(5) <> "" Then

Set dbs = CurrentDb
DA = Forms!frm_Assess!txt_DA
DAX = """" & DA & """"

'Query SQL String for Checks
strSQLA = "SELECT tbl_DA.DAID, tbl_DA.[DA No], tbl_DAConCheck.CheckTitle, tbl_DAConCheck.CheckOutcome, tbl_DAConCheck.CheckComments, tbl_DAConCheck.ConditionCategory, tbl_DAConCheck.Order " & _
"FROM tbl_DA INNER JOIN tbl_DAConCheck ON tbl_DA.DAID = tbl_DAConCheck.DAID " & _
"WHERE (((tbl_DA.[DA No])=" & DAX & ") AND ((tbl_DAConCheck.CheckOutcome)<> ""Invisible"")) " & _
"ORDER BY tbl_DAConCheck.ConditionCategory, tbl_DAConCheck.Order;"
'Query SQL String for RAI
strSQLB = "SELECT tbl_DA.DAID, tbl_DA.[DA No], tbl_DAConCheck.RAIOutcome, tbl_DAConCheck.RAITitle, tbl_DAConCheck.RequestAdditionalInformation, tbl_DAConCheck.ConditionCategory, tbl_DAConCheck.Order " & _
"FROM tbl_DA INNER JOIN tbl_DAConCheck ON tbl_DA.DAID = tbl_DAConCheck.DAID " & _
"WHERE (((tbl_DA.[DA No]) = " & DAX & ") And ((tbl_DAConCheck.RAIOutcome) = True)) " & _
"ORDER BY tbl_DAConCheck.ConditionCategory, tbl_DAConCheck.Order;"
'Set Recordsets
Set QueryA = dbs.OpenRecordset(strSQLA)
Set QueryB = dbs.OpenRecordset(strSQLB)
'Open Word
Set objWord = CreateObject("Word.Application")
With objWord
.Visible = True

Set doc = .Documents.Add
doc.SaveAs CurrentProject.Path & "\TestDoc.doc"
End With

'Title/IntroPage
objWord.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
objWord.Selection.ParagraphFormat.SpaceAfter = 0
objWord.Selection.ParagraphFormat.SpaceBefore = 0

'Insert Gray Table
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed

With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With


objWord.Selection.Shading.Texture = wdTextureNone
objWord.Selection.Shading.ForegroundPatternColor = wdColorAutomatic
objWord.Selection.Shading.BackgroundPatternColor = -603937025
objWord.Selection.Font.Name = "Arial"
objWord.Selection.Font.Size = 11
objWord.Selection.Font.Bold = True
objWord.Selection.Font.Italic = False
objWord.Selection.TypeText Text:="Council Report Summary"
objWord.Selection.TypeParagraph
objWord.Selection.Font.Bold = False
objWord.Selection.TypeText Text:="The proposed development application does not comply with the requirements of Council's relevant policies."
objWord.Selection.MoveDown Unit:=wdLine, Count:=1
objWord.Selection.Shading.Texture = wdTextureNone
objWord.Selection.Shading.ForegroundPatternColor = wdColorAutomatic
objWord.Selection.Shading.BackgroundPatternColor = wdColorAutomatic

objWord.Selection.TypeParagraph
objWord.Selection.TypeParagraph
objWord.Selection.Font.Name = "Arial"
objWord.Selection.Font.Size = 11
objWord.Selection.Font.Bold = True
objWord.Selection.Font.Italic = False
objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
objWord.Selection.TypeText Text:=QueryA!ConditionCategory
objWord.Selection.TypeParagraph
objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
objWord.Selection.Font.Bold = False
objWord.Selection.TypeParagraph
Reply With Quote