Share via

Word visual basic code - working with a table

Anonymous
2014-03-13T16:03:56+00:00

I have a big table in Word that I need to crop, it looks something like this:

asdf asdf asdf asdf asdf
asdf asdf asdf asdf asdf
asdf asdf asdf asdf asdf
asdf asdf sadf Closed Captions Slide number
asdf sadf asdf Hello, how are you? 1
asdf asdf asdf I am fine. 2

What I need is a macro that will delete every row from the top until the row contains either "closed captions" or "slide number" So that the table looks like this:

asdf asdf asdf Closed Captions Slide number
asdf asdf asdf Hello, how are you? 1
asdf asdf asdf I am fine. 2

I should be able to figure out the rest...

It would be nice to add a header to the document that includes the name of the word document. So if the document is "Hello World.doc" the header will say "Hello World"

Thanks a bunch!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2014-03-14T00:47:12+00:00

For the table, you could use code like:

With ActiveDocument.Tables(1)

  While (InStr(.Rows(1).Range.Text, "Closed Captions") = 0) And (InStr(.Rows(1).Range.Text, "Slide number") = 0)

    .Rows(1).Delete

  Wend

End With

For the filename, you could put a FILENAME field in the header. This will show "Hello World.doc". No macros required. If you really don't want the extension, then code will be required, including checking to make sure nothing else in the header is affected.

Was this answer helpful?

0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2015-02-13T23:15:10+00:00

    Formatting in Word documents should be controlled by the use of paragraph Styles. Many people, though, ignore whatever format the underlying paragraph Style has and simply override that with other formatting. That's poor practice and makes documents much harder to maintain. If the cells in column 3 have a particular paragraph Style applied and you want to replicate that in column 2, the process is as simple as selecting the column-2 cells and applying the same paragraph Style. No copying/pasting required. That said, you could use a macro like:

    Sub Demo()

    Application.ScreenUpdating = False

    Dim i As Long

    With ActiveDocument.Tables(1)

      For i = 1 To .Rows.Count

        .Cell(i, 3).Range.Select

        Selection.CopyFormat

        .Cell(i, 2).Range.Select

        Selection.PasteFormat

      Next

    End With

    Application.ScreenUpdating = True

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-02-13T17:18:34+00:00

    Is it OK to ask other questions, Paul? I see you are a MS MVP, I am a physician who tries to use Office in a more powerful way, including working with macros, and I find your support extremely helpful.

    In a table with 3 columns, is there a way to copy the formatting of each cell in column 3 to the same row cells from column 2? I know how to copy formats cell by cell with Ctrl+Shift+C and Ctrl+Shift+V, but when I apply it to a column, it applies the format of the first top cell, so I guess a macro to go cell by cell may do this. It's a long table and would like to save time and copy/paste all cell formats by column.

    Thank you,

    Mike

    Was this answer helpful?

    0 comments No comments