Problem with export table - find/replace a text inside a table in word through excel vba code

elvagonumero1

New Member
Joined
Apr 24, 2012
Messages
15
Hello y'all!

I'm smacking my head down with something I'm pretty sure that's simple, but I couldn't find any proper solution for it

I made a code that succesfully paste an excel table to word, this table has four strings with differents text formats an each cell, after pasting I'd like to find the first of four strings ("FOTO") and replace it with a picture stored in the sheet1 of my excel workbook.

The problems are:
1. The pasting starts fine until page 2 where only pastes the table with content skipping the format.
2. I can't find the way to find, using the find method, the "FOTO" string so after selecting it, it'll be overwritten with the Picture mentioned before. I don't know if this is happening because they all it's inside a table.

Here is a piece of the code, give a look please:

Code:
If Uf Mod 2 = 0 Then 'Knows itf last row is odd or even
        j = Uf \ 2
    Else
        j = Uf \ 2 + 1
    End If
    
    For i = 1 To j
        Ws.Range(Ws.Cells((i - 1) * 2 + 1, 1), Ws.Cells(i * 2, 2)).Copy 'takes a 2x2 table from excel to be pasted in word
        
'---------
'WORD
'---------

        .Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False 'this supposed to paste
                                                                'keeping  origin's format, this works Okay for the first page only
        
        Application.CutCopyMode = False
       
        Worksheets("sheet1").Shapes("Picture 2").CopyPicture 'copy the picture that is going to replace the "FOTO" string
        
        WordDoc.Content.Find.ClearFormatting

        With WordDoc.Content.Find 'I've made three differents alternative of this but any didn't work 
            .Text = "FOTO"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With

        WordDoc.Content.Find.Execute

        Selection.PasteAndFormat (16)
    
    Next i

    'after this the word document is save and blah blah blah!!

I hope someone can help me

Thanks is advance
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello!

I still have both unsolved situations but I've bypassed the pasting issue making each pasted section on a different word document... A very undesired solution but worked.

Now, I've to replaced the word "foto" inside of each cell of a 2x2 table within the word document while it's being created, with an image stored on sheet1 of excel's document.

I've tried some methods found out there but it seems that they don't apply to captions inside a table.

Thanks in advance

Antonio
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top