Fromlostdays1
New Member
- Joined
- Jul 18, 2022
- Messages
- 14
- Office Version
- 365
- Platform
- Windows
Good evening!
I have been working on an Excel booklet that manipulates a Word template I created based on the value of the Excel cells. Basically, the code opens a copy of my Word template (leguinst.docx), replaces some text with cell values, and deletes some bookmarks based on cell values. The problem is, it's an enormous project and I think I hit some kind of limit. If I try to run the code all in one command button now, it errors out saying it can't compile this much.
As a workaround, (I'm hoping) I can just split the code across 2 (or more) command buttons. Please let me know if this is not going to work, haha.
But in doing so I have the problem of targeting the newly created word document with the second command button. Heres my code as it is:
So the first command button opens LegUINST.docx, and replaces all instances of [Plan Administrator] correctly with whatever value I type in Excel.
.
I'm running into all kinds of trouble with the second command button, though. At this point, with the above permutation, I dim wddoc as object and I then I set wddoc as Get Word Application, but to odd behavior. Firstly, the debugger is now catching the erase bookmark section of the code, that has always worked previously and flagging it yellow, and secondly I'm not sure if its even trying to run the code in the now already opened LegUINST.docx document. So again, for the second command button, how would I code it so that it looks for an already opened
I have been working on an Excel booklet that manipulates a Word template I created based on the value of the Excel cells. Basically, the code opens a copy of my Word template (leguinst.docx), replaces some text with cell values, and deletes some bookmarks based on cell values. The problem is, it's an enormous project and I think I hit some kind of limit. If I try to run the code all in one command button now, it errors out saying it can't compile this much.
As a workaround, (I'm hoping) I can just split the code across 2 (or more) command buttons. Please let me know if this is not going to work, haha.
But in doing so I have the problem of targeting the newly created word document with the second command button. Heres my code as it is:
VBA Code:
Dim wdapp As Object
Dim wddoc As Object
Dim planadmin As String
planadmin = Range("D7").Value
Dim capitaladmin As String
capitaladmin = UCase(Range("D7").Value)
Dim adminaddress As String
adminaddress = Range("D8").Value
Dim plantype As String
plantype = Range("D9").Value
Dim capitalplantype As String
capitalplantype = UCase(Range("D9").Value)
Dim plancompany As String
plancompany = Range("D10").Value
Dim plannetwork As String
plannetwork = Range("D11").Value
Set wdapp = CreateObject("Word.application")
wdapp.Visible = True
Set wddoc = wdapp.Documents.Open(ThisWorkbook.Path & "\leguinst.docx")
'Replace Plan Administrator with Cell Value
With wdapp
With wddoc.Content.Find
.Text = "[Plan Administrator]"
.replacement.Text = planadmin
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.Execute Replace:=wdReplaceAll
End With
End With
End Sub
Private Sub CommandButton2_Click()
Dim wddoc As Object
Set wddoc = GetObject(, "Word.Application")
Select Case Range("E23").Value
Case "ISBLANK"
Case "Yes"
Case "N/A"
If wddoc.Bookmarks.Exists("pstsn1") = True Then
wddoc.Bookmarks.Item("pstsn1").Range.Delete
End If
End Select
So the first command button opens LegUINST.docx, and replaces all instances of [Plan Administrator] correctly with whatever value I type in Excel.
.
I'm running into all kinds of trouble with the second command button, though. At this point, with the above permutation, I dim wddoc as object and I then I set wddoc as Get Word Application, but to odd behavior. Firstly, the debugger is now catching the erase bookmark section of the code, that has always worked previously and flagging it yellow, and secondly I'm not sure if its even trying to run the code in the now already opened LegUINST.docx document. So again, for the second command button, how would I code it so that it looks for an already opened