VBA Copy Word File Data To Excel

ranjith2523

Board Regular
Joined
Apr 29, 2014
Messages
137
Office Version
  1. 365
Hello Experts,

Yes, i understand we have multiple threads with the same topic. Before i posting this question i reviewed most of the thread but i didn't get an expected solution.

I am using Office 365 and I just tried the below coding and either it is running for a long time or getting error at "objWord.Selection.WholeStory".

My basic expectation is to simply copy the entire word file (INCLUDING HEADER) and paste it in a specific Tab. (As how we manually do it from Word to Excel)

I would be very helpful for me if anyone help me on this.

VBA Code:
Sub ImportSectHWord()


Dim objWord As Object
Dim objDoc As Object
Dim wdFileName


Set objWord = CreateObject("word.Application")
wdFileName = Application.GetOpenFilename("Word Documents, *.doc*")


If wdFileName = False Then Exit Sub


Set objDoc = GetObject(wdFileName)


objWord.Documents.Open (wdFileName)
objWord.Selection.WholeStory
Selection.Copy


Thisworkbook.sheets("Word").Range("A1").Select
ActiveSheet.Paste


objDoc.Close SaveChanges:=wdDoNotSaveChanges
objWord.Quit


End Sub

Regards,
Ranjith
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try this:

VBA Code:
Sub ImportSectHWord()
  Dim WordApp As Object
  Dim objDoc As Object
  Dim wdFileName As Variant
  
  wdFileName = Application.GetOpenFilename("Word Documents, *.doc*")
  If wdFileName = False Then Exit Sub
  Set WordApp = CreateObject("Word.Application")
  Set objDoc = WordApp.Documents.Open(wdFileName)
      
  WordApp.Selection.WholeStory
  WordApp.Selection.Copy
  
  ThisWorkbook.Sheets("Word").Range("A1").Select
  ActiveSheet.Paste
  objDoc.Close False
  WordApp.Quit
  
  Set WordApp = Nothing
  Set objDoc = Nothing
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub ImportSectHWord()
  Dim WordApp As Object
  Dim objDoc As Object
  Dim wdFileName As Variant
 
  wdFileName = Application.GetOpenFilename("Word Documents, *.doc*")
  If wdFileName = False Then Exit Sub
  Set WordApp = CreateObject("Word.Application")
  Set objDoc = WordApp.Documents.Open(wdFileName)
     
  WordApp.Selection.WholeStory
  WordApp.Selection.Copy
 
  ThisWorkbook.Sheets("Word").Range("A1").Select
  ActiveSheet.Paste
  objDoc.Close False
  WordApp.Quit
 
  Set WordApp = Nothing
  Set objDoc = Nothing
End Sub

Thank you DanteAmor !!!

Yes, you coding is copying the entire content from the work document and pasting it into the tab "Word". From below screen shot, your coding is copying the data from (Please see...

But my concern here is, the coding should copy the Header data too. Is it possible to copy and bring the Header data to Excel Word Tab ? Please advise !!!

1623071397993.png
 
Upvote 0
Thank you DanteAmor !!!

Yes, you coding is copying the entire content from the work document and pasting it into the tab "Word". From below screen shot, your coding is copying the data from (Please see...

But my concern here is, the coding should copy the Header data too. Is it possible to copy and bring the Header data to Excel Word Tab ? Please advise !!!

View attachment 40302

I tried to add the below coding in your macro to just copy the Header from first page of the Word file and paste it in "Word" tab in cell K1. Actually, i want to copy the header just only from the first page of word file and paste it in Cell K1 in "Word" tab.

VBA Code:
Dim docSource As Document

Set docSource = wdFileName
docSource.Sections(i).Headers(j).Range.Copy
         ThisWorkbook.Sheets("Word").Range("K1").PasteSpecial (xlValues)
 
Upvote 0
Headers and footers are a bit tricky to read. Check the following code to access them.


 
Upvote 0
Depending on what you're trying to achieve, you might also consider embedding the document in the workbook.
 
Upvote 0
i want to copy the header just only from the first page of word file and paste it in Cell K1
Try this:

Rich (BB code):
Sub ImportSectHWord()
  Dim WordApp As Object, objDoc As Object
  Dim wdFileName As Variant
  
  wdFileName = Application.GetOpenFilename("Word Documents, *.doc*")
  If wdFileName = False Then Exit Sub
  Set WordApp = CreateObject("Word.Application")
  Set objDoc = WordApp.Documents.Open(wdFileName)
  WordApp.Visible = True
  
  WordApp.Selection.WholeStory
  WordApp.Selection.Copy
  ThisWorkbook.Sheets("Word").Range("A1").PasteSpecial xlPasteValues
  
  WordApp.ActiveDocument.Sections(1).Headers(1).Range.Copy
  ThisWorkbook.Sheets("Word").Range("K1").PasteSpecial xlPasteValues
  
  objDoc.Close False
  WordApp.Quit
  
  Set WordApp = Nothing
  Set objDoc = Nothing
End Sub
 
Upvote 0
Y
Try this:

Rich (BB code):
Sub ImportSectHWord()
  Dim WordApp As Object, objDoc As Object
  Dim wdFileName As Variant
 
  wdFileName = Application.GetOpenFilename("Word Documents, *.doc*")
  If wdFileName = False Then Exit Sub
  Set WordApp = CreateObject("Word.Application")
  Set objDoc = WordApp.Documents.Open(wdFileName)
  WordApp.Visible = True
 
  WordApp.Selection.WholeStory
  WordApp.Selection.Copy
  ThisWorkbook.Sheets("Word").Range("A1").PasteSpecial xlPasteValues
 
  WordApp.ActiveDocument.Sections(1).Headers(1).Range.Copy
  ThisWorkbook.Sheets("Word").Range("K1").PasteSpecial xlPasteValues
 
  objDoc.Close False
  WordApp.Quit
 
  Set WordApp = Nothing
  Set objDoc = Nothing
End Sub
Yes this is what i expected.. Thank you so much for your timely help :)
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

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