Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-08-2018, 08:05 AM
Plycc100no Plycc100no is offline Can I clear the contents of a table using VBA Windows 10 Can I clear the contents of a table using VBA Office 2013
Novice
Can I clear the contents of a table using VBA
 
Join Date: Jun 2018
Posts: 1
Plycc100no is on a distinguished road
Default Can I clear the contents of a table using VBA

Hi - I'm used to using VBA in excel but new to Word although I appreciate they should be similar.

My issue is that I have a word doc with VBA that sends the doc as an attachment through outlook. I've copied and pasted it but that bit seems to work ok.

The issue is that it is not sending through any amended contents (which I believe is due to the doc not being saved before emailed) so what I was going to do was add a line in to save the doc and then attach.



The doc contains two separate tables which I would like to know if I can clear the contents of 1 column in after the document is added to the email so I can save again in its original state.

ie
user completes tables and runs VBA
document saved
added to email
clears contents of table
document saves

I can't seem to work out how I would reference the 2 tables to clear the contents. In excel I would name a range etc but can't see how to do similar in Word.

My current code is

HTML Code:
Private Sub CommandButton1_Click()
Dim olkApp As Object
Dim strSubject As String
Dim strTo As String
Dim strBody As String
Dim strAtt As String
    strSubject = "Testing"
    strBody = "<p style='font-family:arial;font-size:12pt'>" & "Please see the attached document." & "</p>"
    strTo = xxxxxxxxx@xxxxxx.com
           
    strAtt = ActiveDocument.FullName
    
    Set olkApp = CreateObject("outlook.application")
    With olkApp.CreateItem(0)
        .OriginatorDeliveryReportRequested = True
        .To = strTo
        .Subject = strSubject
        .HTMLbody = strBody & vbCrLf & vbCrLf
        .attachments.Add strAtt
        '.send
        .Display
        
        
    End With
    
    ActiveDocument.Close _
 SaveChanges:=wdSaveChanges, _
 OriginalFormat:=wdOriginalDocumentFormat
    Set olkApp = Nothing
End Sub
As I say, this was "borrowed" from something else so any advice that could be given to improve etc would be gratefully received.
Reply With Quote
  #2  
Old 06-08-2018, 08:40 AM
d4okeefe d4okeefe is offline Can I clear the contents of a table using VBA Windows 10 Can I clear the contents of a table using VBA Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

To clear contents of the tables, I think you have to use a for loop, accessing each cell. Something like this:
Code:
    Dim t As Table
    Set t = ActiveDocument.Tables(1)
    Dim i As Integer, j As Integer
    For i = 1 To t.Rows.count
        For j = 1 To t.Columns.count
            t.Cell(i, j).Range.Text = ""
        Next j
    Next i
You may not need this though. You could just use a template, or a dotx file. The original would always have a cleared table. However, the user-filled doc would need to be saved somewhere.
Reply With Quote
  #3  
Old 06-08-2018, 01:32 PM
slaycock slaycock is offline Can I clear the contents of a table using VBA Windows 7 64bit Can I clear the contents of a table using VBA Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

Just be aware that the code above will not work if you have any merged cells in your table.
Reply With Quote
  #4  
Old 06-08-2018, 04:54 PM
macropod's Avatar
macropod macropod is offline Can I clear the contents of a table using VBA Windows 7 64bit Can I clear the contents of a table using VBA Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Instead of trying to clear content from the table, you should be using a Word template that already has the relevant cells cleared, then creating new documents from that template.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I clear the contents of a table using VBA Table of contents JasonMichelM Mail Merge 4 09-15-2017 06:31 PM
Creating a Table of Contents in a table format Ellymoo Word 9 06-22-2017 08:15 AM
Table of contents with section breaks (text in table in header) Dmonk Word 2 05-23-2017 04:29 AM
Can I clear the contents of a table using VBA Move table cell contents from one table to another table or cell in same table donaldadams1951 Word VBA 4 02-04-2015 03:54 PM
Can I clear the contents of a table using VBA Table with Table of Contents as a Column dynamictiger Word Tables 4 07-21-2014 10:16 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:44 AM.


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