Newbie with VBA needs help

Eslava

Board Regular
Joined
Nov 20, 2007
Messages
112
I wrote the following code in order to transfer data from excel to a word document:

Sub worddoc()

Dim objectword As Word.Document
Dim appWD As Word.Application
Dim filename2 As String




Set appWD = New Word.Application

Calculate

filename2 = Worksheets("file_inputs").Range("directory2").Value & "file"




Set objectword = appWD.Documents.Add(filename2)

objectword.Bookmarks("current_compa").Range.Text = Format(Worksheets("data").Range("current_compa").Value, "00.0%")
objectword.Bookmarks("currentnew_compa").Range.Text = Format(Worksheets("data").Range("currentnew_compa").Value, "00.0%")
objectword.Bookmarks("average_tenure").Range.Text = Format(Worksheets("data").Range("average_tenure").Value, "0.0")
objectword.Bookmarks("median_tenure").Range.Text = Format(Worksheets("data").Range("median_tenure").Value, "0.0")
objectword.Bookmarks("min_emp").Range.Text = Format(Worksheets("data").Range("min_emp").Value, "0")
objectword.Bookmarks("min_percent").Range.Text = Format(Worksheets("data").Range("min_percent").Value, "00.0%")
objectword.Bookmarks("low_emp").Range.Text = Format(Worksheets("data").Range("low_emp").Value, "0")
objectword.Bookmarks("low_percent").Range.Text = Format(Worksheets("data").Range("low_percent").Value, "00.0%")
objectword.Bookmarks("mid_emp").Range.Text = Format(Worksheets("data").Range("mid_emp").Value, "0")
objectword.Bookmarks("mid_percent").Range.Text = Format(Worksheets("data").Range("mid_percent").Value, "00.0%")
objectword.Bookmarks("high_emp").Range.Text = Format(Worksheets("data").Range("high_emp").Value, "0")
objectword.Bookmarks("high_percent").Range.Text = Format(Worksheets("data").Range("high_percent").Value, "00.0%")
objectword.Bookmarks("max_emp").Range.Text = Format(Worksheets("data").Range("max_emp").Value, "0")
objectword.Bookmarks("max_percent").Range.Text = Format(Worksheets("data").Range("max_percent").Value, "00.0%")
objectword.Bookmarks("total_emp").Range.Text = Format(Worksheets("data").Range("total_emp").Value, "0")
objectword.Bookmarks("total_percent").Range.Text = Format(Worksheets("data").Range("total_percent").Value, "00.0%")
objectword.Bookmarks("overall_compa").Range.Text = Format(Worksheets("data").Range("overall_compa").Value, "00.0%")

appWD.Visible = True




End Sub



However, I keep getting an error here:
Set objectword = appWD.Documents.Add(filename2)

I have no clue what the error is and I'm looking for ideas as to how to troubleshoot this error.

Any and all help is welcome!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Figured out that problem, now it shows: run time error'9'

This part of the code is highlighted:
objectword.Bookmarks("current_compa").Range.Text = Format(Worksheets("data").Range("current_compa").Value, "00.0%")

Thank you in advance for your help!
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

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