Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-15-2014, 09:27 AM
gmaxey gmaxey is offline Excel to Word and delete certain tables Windows 7 32bit Excel to Word and delete certain tables Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,635
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I agree with Graham. You don't need any bookmarks if you are writing data to table cell. However, as your objective tables (table 8-17) could be more than simply basic rows and columns and creating them with code can get gnarly very quickly, I would do it a little differently.



In your template create the first 7 tables and the first objective table (table 8). Select the objective table and create with it a building block. Call it "Objective Table" and save it to the template.

Then use code something like this:

Code:
Sub Example()
Dim AppWord As Object
Dim oDoc As Object
Dim oTbl As Object
Dim oRng As Object
Dim lngIndex As Long, lngObjectives As Long
  On Error Resume Next
  Set AppWord = GetObject(, "Word.Application")
  If Err Then
      Set AppWord = CreateObject("Word.Application")
  End If
  On Error GoTo 0
  Set oDoc = AppWord.Documents.Add("D:\Demo Template.dotm")
  'Write the first objective.
  Set oTbl = oDoc.Tables(8)
  With oTbl
    .Cell(2, 2).Range.Text = "Some text"
    .Cell(4, 1).Range.Text = "Some other text"
    'etc.
  End With
  'Are there other objectives? Yes in this demo 5
  lngObjectives = 5 'Replace with whatever you have now that determines the number of objectives required.
  For lngIndex = 2 To lngObjectives
    Set oRng = oTbl.Range
    oRng.Collapse wdCollapseEnd
    oRng.MoveEnd wdCharacter, 1
    oRng.InsertBefore vbCr
    oRng.MoveStart wdCharacter, 1
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Objective Table").Insert Where:=oRng
    'Write data to new objective as required.
    Set oTbl = oRng.Tables(1)
    With oTbl
      .Cell(2, 2).Range.Text = "Some text"
      .Cell(4, 1).Range.Text = "Some other text"
      'etc.
    End With
  Next lngIndex
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #2  
Old 10-15-2014, 11:14 AM
jon12355 jon12355 is offline Excel to Word and delete certain tables Windows 7 64bit Excel to Word and delete certain tables Office 2007
Novice
Excel to Word and delete certain tables
 
Join Date: Oct 2014
Posts: 5
jon12355 is on a distinguished road
Default

Thank you for your replies.

gmayor, I used your code, I can get it to work however it is still creating the tables regardless of if the excel data is blank. The way I have the tables set up is:

Objective 1
Heading: l Excel Data enter here
Heading: l Excel Data enter here
Heading: l Excel Data enter here

So with your code I have changed it to:

Code:
Sub Example()
Dim AppWord As Object
Dim WordDoc As Object
Dim oTable As Object
Dim oRng As Object
Dim oCell As Object
Dim ExcSheet As Excel.Worksheet
Dim i As Integer

    On Error Resume Next
    Set AppWord = GetObject(, "Word.Application")
    If Err Then
        Set AppWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    AppWord.Visible = True
    'Set WordDoc = AppWord.Documents.Add("C:\Path\DocName.docx")
    Set WordDoc = AppWord.activedocument
    'Set a range to the table before the place to insert a new table
    Set oRng = WordDoc.Tables(7).Range
    'collapse the range to its end
    oRng.collapse 0
    'Add an empty paragraph to the range
    oRng.Text = vbCr
    'Collapse the range to the end of the empty paragraph
    oRng.collapse 0
    'and add a table at the range
    Set oTable = WordDoc.Tables.Add(Range:=oRng, NumRows:=10, NumColumns:=2)
    'Now fill the table
    Set oCell = oTable.Cell(1, 1).Range        'Row,Column
    oCell.End = oCell.End - 1
    oCell.Text = "Objective 1"
    Set oCell = oTable.Cell(1, 2).Range
    oCell.End = oCell.End - 1
    oCell.Text = ""
Set oCell = oTable.Cell(2, 1).Range        'Row,Column
    oCell.End = oCell.End - 1
    oCell.Text = "Heading:"
    Set oCell = oTable.Cell(2, 2).Range
    oCell.End = oCell.End - 1
    oCell.Text = Excel Textbox.Value
    'Set oRng to oTable.Rang
    'and repeat the above section for each new table depending on the coindition that determines whether a table is required.
End Sub
When I press the command button it still creates the tables regardless of if the boxes in excel are blank or have text in. Am i doing something wrong?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel to Word and delete certain tables Blury Excel Tables in Word zachg18 Word 1 07-15-2013 11:10 PM
how to copy all ms word tables into excel rehan129 Word 0 01-28-2012 10:17 AM
Excel to Word and delete certain tables Excel Tables to MS Word ripcurlksm Word Tables 2 09-09-2011 04:59 AM
Excel to Word and delete certain tables using checkboxes to delete tables atfresh Word Tables 1 06-19-2011 09:13 PM
Excel to Word and delete certain tables working with excel tables in MS word radman154 Word Tables 1 03-25-2011 12:04 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:52 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft