PDA

View Full Version : Access to Populate Word Document - Selection.InlineShapes.AddPicture only works once



HokieCode
09-15-2015, 06:28 AM
Hello everyone. New poster here. I am creating a program in Access (Office 2010) that pulls info from a database and populates a word document form letter. The users are able to select their name from a dropdown that also populates their signature.

The last thing I'm doing is to insert an image of the user's signature. I've gotten this to work using Selection.InlineShapes.AddPicture

However, I changed the code to now reference a string for the image path (which changes depending on the user selected) and now it only works the first time I run the script. Every other time it populates the document, but not with the signature. No errors pop up. If I completely stop and restart the vba code it works again.

Any help would be appreciated.

Here is the code for the function that fills out the letter:


Function fillCostLetter()
Dim appword As Word.Application
Dim doc As Word.Document
Dim Path As String
TodayDate = Format(Now(), "mmmm dd, yyyy")
On Error Resume Next
Error.Clear


Set appword = GetObject(, "word.application")


If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True
End If


Path = "Z:\PathtoDoc\ServiceAssociateToolBox\CostLetterTestStage.docx"


Set doc = appword.Documents.Open(Path, , True)


With doc
.FormFields("Date").Result = TodayDate
.FormFields("BillName").Result = BillName
.FormFields("BillAmmount").Result = BillAmmount
.FormFields("BillAddress").Result = BillAddress
.FormFields("BillAmmount").Result = BillAmmount
.FormFields("BillCity").Result = BillCity
.FormFields("BillState").Result = BillState
.FormFields("BillZip").Result = BillZip


.FormFields("SiteZip").Result = SiteZip
.FormFields("SiteState").Result = SiteState
.FormFields("SiteCity").Result = SiteCity
.FormFields("SiteStreetType").Result = SiteStreetType
.FormFields("SiteStreetName").Result = SiteStreetName
.FormFields("SiteStreetNo").Result = SiteStreetNo
.FormFields("BillName2").Result = BillName
.FormFields("WorkRequest").Result = WR_NO

.FormFields("CustName").Result = CustName


.FormFields("SAName").Result = SAName
.FormFields("SADeptartment").Result = SADept
.FormFields("SAPhone").Result = SAPhone




'' Add signature


Selection.GoTo What:=wdGoToBookmark, Name:="Signature"
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.InlineShapes.AddPicture FileName:= _
SASignaturePath, _
LinkToFile:=False, SaveWithDocument:=True

'' end signature stuff


End With


appword.Visible = True
appword.Activate


'' clear stuff


Set doc = Nothing
Set appword = Nothing


End Function

HokieCode
09-15-2015, 07:23 AM
Quick update. I noticed that with the code above- If I leave the first document open and run a second time, it will create a second document and populate but add the image to the first document.

I tried doing a
Selection = ActiveDoument
hoping that it would select the new document but it didn't work.