VBA to save results to existing workbook instead of new workbook.

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello Dear community,

I have a VBA workbook which copy data from Word document and saves it to new excel file.
Can anybody help me to save it to existing workbook instead of new workbook?

VBA Code:
For Each f In fo.Files
    Set doc = wa.documents.Open(f.Path, False, Format:="PDF Files")
    Set wr = doc.Paragraphs(1).Range
    wr.WholeStory
    
    
    Set nwb = Workbooks.Add
    Set nsh = nwb.Sheets(1)
    wr.Copy
    
    nsh.Paste
    nwb.SaveAs (excel_path & "\" & Replace(f.Name, ".pdf", ".xlsx"))
    
    doc.Close False
    nwb.Close False
Next
Thank you.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Maybe:
VBA Code:
For Each f In fo.Files
    Set doc = wa.documents.Open(f.Path, False, Format:="PDF Files")
    Set wr = doc.Paragraphs(1).Range
    wr.WholeStory
   
   
    Set twb = ThisWorkbook
    Set tsh = twb.Sheets(1)
    wr.Copy
    tsh.Paste
    doc.Close False
Next
 
Upvote 0
Or maybe you would want to create a sheet for each file imported?
VBA Code:
For Each f In fo.Files
    Set doc = wa.documents.Open(f.Path, False, Format:="PDF Files")
    Set wr = doc.Paragraphs(1).Range
    wr.WholeStory
    
    
    Set twb = ThisWorkbook
    Set tsh = twb.Sheets.Add
    wr.Copy
    tsh.Paste
    doc.Close False
Next
 
Upvote 0
Thank you both for fast reply. Yes I try to do it in workbook which will run VBA. I will try code and tell you if it works.

"Or maybe you would want to create a sheet for each file imported?"
Would be great..
 
Upvote 0
In that case the code from Georgiboy in post#3 should do the trick.
 
Upvote 0
Not worked sadly.. I guess it will be better if i share full VBA , may be something missing:

VBA Code:
Option Explicit

Sub PDF_To_Excel()


Dim setting_sh As Worksheet
Set setting_sh = ThisWorkbook.Sheets("Setting")

Dim pdf_path As String
Dim excel_path As String

pdf_path = setting_sh.Range("K11").Value
excel_path = setting_sh.Range("K12").Value


Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File

Set fo = fso.GetFolder(pdf_path)

Dim wa As Object
Dim doc As Object
Dim wr As Object

Set wa = CreateObject("word.application")
 


'Dim wa As New Word.Application
wa.Visible = True
'Dim doc As Word.Document

Dim nwb As Workbook
Dim nsh As Worksheet
'Dim wr As Word.Range


For Each f In fo.Files
    Set doc = wa.documents.Open(f.Path, False, Format:="PDF Files")
    Set wr = doc.Paragraphs(1).Range
    wr.WholeStory
    
    
    Set twb = ThisWorkbook
    Set tsh = twb.Sheets.Add
    wr.Copy
    tsh.Paste
    doc.Close False
Next

wa.Quit

MsgBox "Done"


End Sub

After Word file opened I am getting , notification message : This can take a while etc...
Is it possible to ignore that message?
 
Upvote 0
Hmm i have had a play with the code but not changed much, just made it easier for me to read and added a dim

VBA Code:
Sub PDF_To_Excel()
    Dim setting_sh As Worksheet
    Dim pdf_path As String
    Dim excel_path As String
    Dim fso As New FileSystemObject
    Dim fo As Folder
    Dim f As File
    Dim wa As Object
    Dim doc As Object
    Dim wr As Object
    Dim twb As Workbook
    Dim tsh As Worksheet
   
    Set twb = ThisWorkbook
    Set setting_sh = twb.Sheets("Setting")
    pdf_path = setting_sh.Range("K11").Value
    Set fo = fso.GetFolder(pdf_path)
    excel_path = setting_sh.Range("K12").Value
    Set wa = CreateObject("word.application")
    wa.Visible = True
   
    For Each f In fo.Files
        Set doc = wa.documents.Open(f.Path, False, Format:="PDF Files")
        Set wr = doc.Paragraphs(1).Range
        wr.WholeStory
        Set tsh = twb.Sheets.Add
        wr.Copy
        tsh.Paste
        doc.Close False
    Next
    wa.Quit
    MsgBox "Done"
End Sub

As for that message box you are getting - i think the message box has a bit to tick to say don't show again.
 
Upvote 0
You will need to add a reference in the VBE for: Mirosoft Scripting Runtime
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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