How to name new sheets from opened .docx filename

JohanPoulsen

New Member
Joined
Jan 29, 2018
Messages
4
Searching the forums I have found this useful code:

Code:
Sub GetWordDocContents()
  Dim oWord As Object
  Dim vFiles
  Dim iFile As Integer
  Dim R As Range
  vFiles = Application.GetOpenFilename("Word files (*.doc*),*.doc*", Title:="Please select the files you want to copy from", MultiSelect:=True)
  If TypeName(vFiles) = "Boolean" Then Exit Sub ' Cancelled
  Set oWord = CreateObject("Word.Application")
  oWord.Visible = True
  Set R = Worksheets.Add.Range("A1")
  For iFile = LBound(vFiles) To UBound(vFiles)
    oWord.Documents.Open vFiles(iFile)
    oWord.ActiveDocument.Tables(1).Select
    oWord.Selection.Copy
    ActiveSheet.Paste R
    Set R = Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1)
    oWord.ActiveDocument.Close False
  Next
  oWord.Quit
  Set oWord = Nothing
  ActiveSheet.Columns.AutoFit


End Sub
I want to change it so that it creates an individual sheet for each word wile, while naming each new sheet with part of the wordfiles filename.

Filenames are: "Formativ feedbackskema dansk.docx", "Formativ feedbackskema engelsk.docx", and so on. I want to name each sheet with the last word: "dansk", "engelsk", and so on.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I forgot to say: The above code opens multiple word documents, copies tables in them, and pastes the tables into a new worksheet in excel.
 
Upvote 0

Forum statistics

Threads
1,223,768
Messages
6,174,414
Members
452,562
Latest member
Himeshwari

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