Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find text on word document inside textbox

Posted on 2014-04-17
2
Medium Priority
?
617 Views
Last Modified: 2014-04-17
Hi

I'm trying to find text inside a textbox in a word document.
I've tried everything, but could not get it to highlight the word that I've searched for.
I want to add a logo where the word [LOGO] is in the document.
The replace all works, but I don't want to replace it, I want to find each word and add a logo picture in it's place.

Private Sub cmd1_Click(sender As Object, e As EventArgs) Handles cmd1.Click
    Const wdFindContinue = 1
    Const wdMainTextStory = 1
    Dim oApp As Object = CreateObject("Word.Application")
    Dim oDoc As Object
    oApp.visible = True
    oDoc = oApp.Documents.open("C:\doc.DOC")
    Dim oStory As Object
    For Each oStory In oDoc.StoryRanges
      If oStory.StoryType <> wdMainTextStory Then
        While Not (oStory.NextStoryRange Is Nothing)
          With oStory.Find
            .Text = "[LOGO]"
            .Replacement.Text = ""
            .Wrap = wdFindContinue
            .Execute()
            MsgBox(oApp.selection.text)
          End With
          oStory = oStory.NextStoryRange
        End While
      End If
    Next oStory
    oDoc = Nothing
    oApp = Nothing
  End Sub
End Class

Open in new window

0
Comment
Question by:koossa
  • 2
2 Comments
 
LVL 15

Accepted Solution

by:
DrTribos earned 2000 total points
ID: 40006238
Text box is a shape, I think you have to look inside each shape...

Sub shTest()

Dim sh As Shape

For Each sh In ActiveDocument.Shapes
Debug.Print sh.TextFrame.TextRange.Text
Next sh

End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40006681
Glad it helped :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question