I am trying to insert a shape.textbox into a word doc. If it is already there I would like to clear out any text that is already in there and write new text in the textbox. If it is not already there, I would like to create it so I can put the text inside it.
I have been able to create the textbox and fill it with text, but if I access the document again, I end up with multiple instances of the textbox as it is recreated each time.
TellDerek2@GMail.com
I can see how that would work. But what if I wanted to remove any duplicate text boxes, then create a new one to write my write my text to.
My concern is that I will end up with too many text boxes on the page.
PS: I’'m using VB.net to write the program.
Hi Derek,
Here is the code above in Visual Basic format for your reference if needed.
Dim doc As New Document(dataDir & "Test Document.doc")
Dim hasTextBox As Boolean = False
Dim textBox As Shape = Nothing
' Try to find existing textbox
Dim nodes As NodeCollection = doc.GetChildNodes(NodeType.Shape, True)
For Each node As Shape In nodes
If node.ShapeType = ShapeType.TextBox Then
hasTextBox = True
textBox = node
End If
Next node
' Create a new textbox if no textbox already exists
If (Not hasTextBox) Then
textBox = New Shape(doc, ShapeType.TextBox)
' Set Textbox size
textBox.Width = 100
textBox.Height = 100
' Insert one empty paragraph into the textbox.
Dim par As New Paragraph(doc)
par.AppendChild(New Run(doc))
textBox.AppendChild(par)
' Insert textbox into the document.
doc.FirstSection.Body.FirstParagraph.AppendChild(textBox)
End If
' Change text in textbox
textBox.FirstParagraph.Runs(0).Text = "Text Inserted"
doc.Save(dataDir & "Test Document Out.doc")
Thanks,
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.