Solved

Find text on word document inside textbox

Posted on 2014-04-17
2
598 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 15

Accepted Solution

by:
DrTribos earned 500 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

RoboForm Secure Password Management System

RoboForm Everywhere - Superb Browser Support
Windows / Apple / IOS / Android / Linux / Chrome OS
Use different complex passwords everywhere
Best Secure Password Management by far
Synchronize all of your devices instantly
Safe, Secure & Highly Recommended!

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

734 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