Hi All! This is my very first post to MrExcel. I love this site and have used it countless times to find solutions to my problems. I think I can finally contribute something that I haven't been able to find anywhere on the www; however, I would like to see if anyone can make my code simpler.
My code below will take values in Excel and copy them to bookmarks in a Word template I have created. The problem I could not find was how to format the bookmark so I could see the value in Word as currency, %, etc. I finally figured it out and that is below. My question is, based on my code below, is there an easer way to do this vs. the way I have the various Set BookmarkRange lines? Any advice would be beneficial. I do not have any formal training in VBA or any programming for that matter so any advice is welcome...
~tbeards
Option Explicit
Sub PasteExcelPricingToWord()
Dim obj As Word.Application
Dim wdDoc As Object
Dim LPRMarkdown As Range
Dim StockFee As Range
Dim CustomerName As Range
Dim AccountManager As Range
Dim BookmarkRange As Word.Range
Dim Workbook As ThisWorkbook
'******** NAME THE EXCEL VALUES TO THE BOOKMARK NAMES ***************
Worksheets(constSheetLPR).Activate 'Activate the worksheet
Set LPRMarkdown = Worksheets(constSheetLPR).Range("H19")
Set StockFee = Worksheets(constSheetLPR).Range("H20")
Set CustomerName = Worksheets(constSheetLPR).Range("C4")
Set AccountManager = Worksheets(constSheetLPR).Range("C5")
'***************** OPEN THE WORD PRICING PROPOSAL TEMPLATE ***************
Set obj = CreateObject("Word.Application.11") 'Create a word object
Set wdDoc = obj.Documents.Open("C:\\Documents and Settings\221008334\Desktop\Pricing_Presentation_test.doc")
obj.Visible = True
'************* INPUT THE EXCEL VALUES TO THE BOOKMARKS & FORMAT **********
Set BookmarkRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="Markdown")
BookmarkRange.Text = Format$(LPRMarkdown, "$#,###")
Set BookmarkRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="StockFee")
BookmarkRange.Text = Format$(StockFee, "0.00%")
Set BookmarkRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="CustomerName")
BookmarkRange.Text = CustomerName
Set BookmarkRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="AccountManager")
BookmarkRange.Text = AccountManager
'*************** SAVE THE WORD DOC WITH THE CUSTOMER NAME ****************
wdDoc.SaveAs Filename:="C:\Pricing\" & CustomerName & " Pricing Proposal.doc" 'Save the file
Set obj = Nothing 'Release object
Set wdDoc = Nothing 'Release object
Call MsgBox("Your Pricing Proposal is complete.", vbOKOnly)
PathErr:
Exit Sub
End Sub
My code below will take values in Excel and copy them to bookmarks in a Word template I have created. The problem I could not find was how to format the bookmark so I could see the value in Word as currency, %, etc. I finally figured it out and that is below. My question is, based on my code below, is there an easer way to do this vs. the way I have the various Set BookmarkRange lines? Any advice would be beneficial. I do not have any formal training in VBA or any programming for that matter so any advice is welcome...
~tbeards
Option Explicit
Sub PasteExcelPricingToWord()
Dim obj As Word.Application
Dim wdDoc As Object
Dim LPRMarkdown As Range
Dim StockFee As Range
Dim CustomerName As Range
Dim AccountManager As Range
Dim BookmarkRange As Word.Range
Dim Workbook As ThisWorkbook
'******** NAME THE EXCEL VALUES TO THE BOOKMARK NAMES ***************
Worksheets(constSheetLPR).Activate 'Activate the worksheet
Set LPRMarkdown = Worksheets(constSheetLPR).Range("H19")
Set StockFee = Worksheets(constSheetLPR).Range("H20")
Set CustomerName = Worksheets(constSheetLPR).Range("C4")
Set AccountManager = Worksheets(constSheetLPR).Range("C5")
'***************** OPEN THE WORD PRICING PROPOSAL TEMPLATE ***************
Set obj = CreateObject("Word.Application.11") 'Create a word object
Set wdDoc = obj.Documents.Open("C:\\Documents and Settings\221008334\Desktop\Pricing_Presentation_test.doc")
obj.Visible = True
'************* INPUT THE EXCEL VALUES TO THE BOOKMARKS & FORMAT **********
Set BookmarkRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="Markdown")
BookmarkRange.Text = Format$(LPRMarkdown, "$#,###")
Set BookmarkRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="StockFee")
BookmarkRange.Text = Format$(StockFee, "0.00%")
Set BookmarkRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="CustomerName")
BookmarkRange.Text = CustomerName
Set BookmarkRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="AccountManager")
BookmarkRange.Text = AccountManager
'*************** SAVE THE WORD DOC WITH THE CUSTOMER NAME ****************
wdDoc.SaveAs Filename:="C:\Pricing\" & CustomerName & " Pricing Proposal.doc" 'Save the file
Set obj = Nothing 'Release object
Set wdDoc = Nothing 'Release object
Call MsgBox("Your Pricing Proposal is complete.", vbOKOnly)
PathErr:
Exit Sub
End Sub