Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2017, 12:55 AM
Fredrik1987 Fredrik1987 is offline Update content in table adds a new column Windows 7 64bit Update content in table adds a new column Office 2010 64bit
Novice
Update content in table adds a new column
 
Join Date: Mar 2017
Posts: 3
Fredrik1987 is on a distinguished road
Default Update content in table adds a new column

Hi all!

This is my first project in Word, as you can see I'm a bit inexperienced
I'm comparing the content in two documents, and replacing/updating the content in Userdocument's tables.

If the table have one row, it inserts a new column instead of updating the content.

My question:
  • The "error" rises when the table has only 1 row, is this some specialcase I'm not aware of?

Code:
'/  UpdateContent in leftmost column
Private Sub CompareAddContent( _
MalCol As Column, _
UserCol As Column _
)


Dim Cell As Cell
Dim row As Integer
Dim cl_Match As Cell
Dim NewRow As row


For Each Cell In MalCol.Cells
    
    Cell.Range.Copy
    
    Set cl_Match = LookForMatch(UserCol, Cell.Range.Text)   '   This function looks for correct content (Works)
    If cl_Match Is Nothing Then
    
        
        '/  Insert new row and add content
        Set NewRow = UserCol.Parent.Rows.Add
        NewRow.Cells(1).Range.PasteAndFormat wdFormatOriginalFormatting
        
        
    Else
        
        
        '/  Replace content in cell
        '/  ***********************
        '/  It works fine if the number of rows > 1
        cl_Match.Range.PasteAndFormat wdFormatOriginalFormatting
        '/  **********************
        
        
    End If
Next


End Sub

Attached Images
File Type: png Skjermbilde.PNG (3.6 KB, 17 views)
Reply With Quote
  #2  
Old 03-15-2017, 11:31 PM
gmayor's Avatar
gmayor gmayor is offline Update content in table adds a new column Windows 10 Update content in table adds a new column Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It is difficult to understand what your code is supposed to do without access to the tables, the values fed to the macro and the additional function.

There is no reference to which table you are working with at any time nor which document you are working in.

However from what you have presented, what is the point of any of the Else branch?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-16-2017, 06:09 AM
Fredrik1987 Fredrik1987 is offline Update content in table adds a new column Windows 7 64bit Update content in table adds a new column Office 2010 64bit
Novice
Update content in table adds a new column
 
Join Date: Mar 2017
Posts: 3
Fredrik1987 is on a distinguished road
Default

I'll try to explain better, Sorry if I'm wasting to much of your time.
My question is: If a table has only 1 row, are there any problems/limitations with the Range.paste method I'm not aware of?


Added a crude code below, hope it explains to some extent what I'm trying to achieve.


Code:
'/  Copy tables from master document to userdoc
Sub CopyTables( _
)

Dim MasterDoc As Word.Document
Dim UserDoc As Word.Document
Dim MyTable As Word.Table
Dim rng_Paste As Variant

Dim MyCell As Word.Cell
Dim i As Integer


Set MasterDoc = Documents("MasterDoc.docx")
Set UserDoc = Documents("UserDoc.docm")


'/  Paste content from tables in MasterDoc to tables in UserDoc
For Each MyTable In MasterDoc.Tables
    i = i + 1
    For Each MyCell In MyTable.Columns(1).Cells
    
        
        Set rng_Paste = UserDoc.Tables(i).Cell(MyCell.RowIndex, 1).Range
        
        
        '/   If the number of rows = 1, it inserts a new column instead of replacing the 
        '/   content in rng_Paste. If number of rows > 1 it replaces the content of
        '/   rng_Paste (As it should)

        MyCell.Range.Copy
        rng_Paste.Paste wdFormatOriginalFormatting
    
    
    Next
Next


End Sub

Regarding the content; It's mainly text/hyperlinks, though it seems like the problems relates to the number of rows in a table, and not the content itself. It works just fine with any table if the number of rows is grater than

As for the if/else, there's two possibilites (As I see them):
  1. The UserTable doesn't contain the information, if so add a new row and copy from masterdocument
  2. The information exist, if so; Replace (Update) the content in UserTable with content from MasterTable
Reply With Quote
  #4  
Old 03-16-2017, 11:03 PM
gmayor's Avatar
gmayor gmayor is offline Update content in table adds a new column Windows 10 Update content in table adds a new column Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

When working with cell ranges, be aware that the cell range includes the cell end character. You are more likely to get the results you are seeking if having set the range, you remove the end character from the range (whether copying or pasting) e.g.
Code:
Set rng_Paste = UserDoc.Tables(i).Cell(MyCell.RowIndex, 1).Range
rng_Paste.End = rng_Paste.End - 1
Note also that there is no need to copy and paste when working with ranges. You can simply write the formatted value of the source range to the target range e.g.
TargetRangeName.FormattedText = SourceRangeName.FormattedText.
Your last example code does not work with two documents and their corresponding tables.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 03-21-2017, 04:59 AM
Fredrik1987 Fredrik1987 is offline Update content in table adds a new column Windows 7 64bit Update content in table adds a new column Office 2010 64bit
Novice
Update content in table adds a new column
 
Join Date: Mar 2017
Posts: 3
Fredrik1987 is on a distinguished road
Default

Thank you so much!
Reply With Quote
Reply

Tags
tables, vba macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop and Update Content Controls with value vss712 Word VBA 7 08-27-2015 08:56 PM
Update fields and Tables of Content with one macro brent chadwick Word VBA 7 03-11-2015 07:21 AM
Update content in table adds a new column Combine Different Word files into one file and update Table of Content tejaspareek Word VBA 4 11-04-2014 05:50 AM
Update content in table adds a new column How to align And center the content of the column in a table? Jamal NUMAN Word 1 05-02-2011 05:47 PM
Update content in table adds a new column SOS - cannot update table of content properly Lee Word 5 02-04-2011 12:59 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:19 PM.


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