Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5952599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:42:16+00:00 2026-05-22T17:42:16+00:00

I don’t have too much experience with VB Scripting but I’m trying to write

  • 0

I don’t have too much experience with VB Scripting but I’m trying to write something that will search for a specific string in a word document, replace it with whatever I specify and then print it out on a label printer.

It does the first replacement just fine, but not the second. Could anyone have a look and see what I might be doing wrong?

Option Explicit

Dim WordApp
Dim WordDoc
Dim strReadCompName
Dim strReadCompSN

Set WordApp = CreateObject("Word.Application")
WordApp.Visible = TRUE

WordApp.Documents.Open("C:\LabelTemplate.doc")
WordApp.Documents("LabelTemplate.doc").Activate

Set WordDoc = WordApp.ActiveDocument

strReadCompName = InputBox("Enter Computer Name", "Name")
strReadCompSN = InputBox("Enter Serial Number", "Serial")

With WordApp.Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWholeWord = TRUE
    .Text = "nametext"
    .Execute ,,,,,,,,,strReadCompName
End With

With WordApp.Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWholeWord = TRUE
    .Text = "serialtext"
    .Execute ,,,,,,,,,strReadCompSN
End With

WordDoc.PrintOut()
WordDoc.Saved = TRUE
WordApp.Quit
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:42:17+00:00Added an answer on May 22, 2026 at 5:42 pm

    Solved my own problem haha

    Oracle Certified Professional was right. All I had to do was add another statement to move the cursor back to the beginning. I just added WordApp.Selection.GoTo 1 to the beginning.

    Option Explicit
    'Procedure to edit word document add name and serial number. 
    Sub SearchAndRep(searchTerm, replaceTerm, WordApp)
        WordApp.Selection.GoTo 1
        With WordApp.Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .MatchWholeWord = True
            .Text = searchTerm
            .Execute ,,,,,,,,,replaceTerm
        End With
    End Sub
    
    Dim WordApp
    Dim WordDoc
    Dim strReadCompName
    Dim strReadCompSN
    Dim objNetwork, WSHPrinters, counter
    
    'Enumerate through printers to find local Zebra printer.
    Set objNetwork = CreateObject("WScript.Network")
    Set WSHPrinters = objNetwork.EnumPrinterConnections
    
    For counter = 0 To WSHPrinters.Count - 1 Step 2
        If Left(WSHPrinters.Item(counter +1), 5) = "Zebra" Then
            WScript.Echo(WSHPrinters.Item(counter +1))
            objNetwork.SetDefaultPrinter(WSHPrinters.Item(counter +1))
        End If
    Next
    
    'Create a Microsoft Word Object and make it invisible. 
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = FALSE
    
    'Open LabelTemplate.doc for editing. 
    Set WordDoc = WordApp.Documents.Open("C:\LabelTemplate.doc")
    
    'Read in name and serial number to print on label. 
    strReadCompName = InputBox("Enter Computer Name", "Name")
    strReadCompSN = InputBox("Enter Serial Number", "Serial")
    
    'Procedures to edit the Word Document to add name and serial number. 
    SearchAndRep "nametext", strReadCompName, WordApp
    SearchAndRep "serialtext", strReadCompSN, WordApp
    
    'Print out the label. 
    'WordApp.PrintOut()
    
    'Set the default printer back to what it was before. 
    'Still have to do this. 
    objPrinter.SetDefaultPrinter "\\******\**********"
    WordDoc.Saved = TRUE
    WordDoc.Close
    WordApp.Quit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DON'T ASK WHY but... I have a regex that needs to be case insensitive
I don't have much PHP experience and I want to know how to best
Don't you hate it when you have class Foobar { public: Something& getSomething(int index)
Don't know if this is an eclipse specific problem but whenever I declare a
I don't have any experience in updating a Rails app and when I google
Don't ask why but I have the requirement to draw a border around certain
We don't have the .ogv format available from the encoder in our system, but
I don't know how jQuery works under the hood, but let's say that at
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know if this is the right place to ask this, but I will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.