How to edit form fields in word update#
“Error Description: ” & Err.How to update fields in a protected form in Word 2007įor a Microsoft Word 2002 and Microsoft Office Word 2003 version of this article, see 291474.įor a Microsoft Word 2000 version of this article, see 212356.Ĭonsider the following scenario. “Error Number: ” & Err.Number & vbCrLf & _ MsgBox “The following error has occured.” & vbCrLf & vbCrLf & _ MsgBox “The specified file ‘” & sDocName & “‘ could not be found.”, _ ODoc.SaveAs FileName:=(ThisWorkbook.Path & “/offerte/” & oDoc.FormFields(“Numero_Offerta”).Result & “_” & oDoc.FormFields(“Name”).Result & “.docx”) ‘oDoc.FormFields(“CheckboxName”).CheckBox.Value = True ‘Checkbox ODoc.FormFields(“Name”).Result = ThisWorkbook.Sheets(“Offerte”).Range(“B” & LastRow).Value ‘Textbox ODoc.FormFields(“Numero_Offerta”).Result = ThisWorkbook.Sheets(“Offerte”).Range(“A” & LastRow).Value ‘Textbox LastRow = ThisWorkbook.Sheets(“Offerte”).Cells.SpecialCells(xlCellTypeLastCell).Row SDocName = (ThisWorkbook.Path & “/modello_offerta.dotx”) Set oApp = CreateObject(“Word.Application”) If Err.Number 0 Then ‘Word isn’t running so start it Set oApp = GetObject(, “Word.Application”) ‘See if word is already running ‘Requires a reference to the Word object library I’ve figured that word file is opened every two times, not alwaysĬlick again on the macro button –> open word, open file, filling text, save file renamed, everything ok
How to edit form fields in word code#
I’ve adjusted the code for my purpose, I have to retrieve the last row of an excel and fill the template word, then save with another name. ODoc.FormFields("CheckboxName").CheckBox.Value = True 'Checkbox Error_Handler_Exit:Įnd Sub Form Field MS Access VBA Programming MS Excel VBA Word 7 responses on “ VBA – Word – Update/Fill-in Document Form Fields” ODoc.FormFields("TextboxName").Result = "NewValue" 'Textbox SDocName = "YourWordDocToOpenFullPathAndExtension" Set oApp = CreateObject("Word.Application") If Err.Number 0 Then 'Word isn't running so start it Set oApp = GetObject(, "Word.Application") 'See if word is already running 'Requires a reference to the Word object libraryĭim sDocName As String On Error Resume Next It may not be sold/resold or reposted on other sites (links ' copyright notice is left unchanged (including Author, Website and ' Copyright : The following may be altered and reused as you wish so long as the ' Purpose : Update/Fill-in a Word document's form fields VbCritical, "An Error has Occurred!" End If Resume Error_Handler_Exit "Error Description: " & Err.Description, _ "Error Number: " & Err.Number & vbCrLf & _ MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
MsgBox "The specified file '" & sDocName & "' could not be found.", _ Set oDoc = Nothing Set oApp = Nothing Exit Sub ODoc.FormFields( "CheckboxName").CheckBox.Value = True 'Checkbox ODoc.FormFields( "TextboxName").Result = "NewValue" 'Textbox SDocName = "YourWordDocToOpenFullPathAndExtension" Set oDoc = oApp.Documents. 'Requires a reference to the Word object library Dim oApp As Object 'Word.Application Dim oDoc As Object 'Word.Document Dim sDocName As String On Error Resume Next Set oApp = GetObject(, "Word.Application") 'See if word is already running If Err.Number 0 Then 'Word isn't running so start it Set oApp = CreateObject( "Word.Application") It may not be sold/resold or reposted on other sites (links ' back to this site are allowed).
' Website : ' Purpose : Update/Fill-in a Word document's form fields ' Copyright : The following may be altered and reused as you wish so long as the ' copyright notice is left unchanged (including Author, Website and ' Copyright). '- ' Procedure : UpdateDoc ' Author : CARDA Consultants Inc.