Solved

VBA to VB Script - Code modify

Posted on 2014-01-21
4
583 Views
Last Modified: 2014-01-21
Hello Experts,

First off, I am new to VBScript so please bare with me.  I have a VBA script in MS Word, that I need to have it recoded to work in a VB script.  I was wondering if someone could help me out.  Here are the 4 scripts:

Option Explicit


Sub SetBold()

    Const Mark As String = "BHN-"
    
    Dim Rng As Range
    Dim Rstart As Long, Rend As Long
    Dim Fnd As Boolean
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, ActiveDocument.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = ActiveDocument.Range(Start:=Rstart + Len(Mark) + 1)
        Rend = GetStart(") -", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = ActiveDocument.Range(Start:=Rstart, End:=Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub
Sub SetRestBold()

    Const Mark As String = "Score:"
    
    Dim Rng As Range
    Dim Rstart As Long, Rend As Long
    Dim Fnd As Boolean
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, ActiveDocument.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = ActiveDocument.Range(Start:=Rstart + Len(Mark) + 1)
        Rend = GetStart(" - ", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = ActiveDocument.Range(Start:=Rstart, End:=Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub
Sub SetBoldLevel6()

    Const Mark As String = "LEVEL 6 Events:"
    
    Dim Rng As Range
    Dim Rstart As Long, Rend As Long
    Dim Fnd As Boolean
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, ActiveDocument.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = ActiveDocument.Range(Start:=Rstart + Len(Mark) + 1)
        Rend = GetStart("LEVEL 6 Events:", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = ActiveDocument.Range(Start:=Rstart, End:=Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub
Sub SetBoldLevel5()

    Const Mark As String = "LEVEL 5 Events:"
    
    Dim Rng As Range
    Dim Rstart As Long, Rend As Long
    Dim Fnd As Boolean
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, ActiveDocument.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = ActiveDocument.Range(Start:=Rstart + Len(Mark) + 1)
        Rend = GetStart("LEVEL 5 Events:", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = ActiveDocument.Range(Start:=Rstart, End:=Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub

Open in new window




Below is the VBScript that I am working with.  Notice I added a section for insert VBA code.

'Language = VBScript
'Script to process, format, and email the Weekly Event Doc


Sub FormatReport

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Documents and Settings\lspaz\Desktop\Test Report Automation\XXX.docx")
***INSERT VBA CODE
objWord.Quit

Open in new window


Since I am only bolding specific text..  Perhasps I should simplify this and write it in VB Script?
Thanks
0
Comment
Question by:Maliki Hassani
  • 2
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39797890
The following shows all working ... of course you will need to pick out the global section everytie you want to run one of the subs but essentially the process is get rid of all typing and reference objects to an instance of word removing all named parameters and using commas where multiple parameters are required to get the right parameter.

Option Explicit

Dim objWord
Dim objDoc

Set objWord = getObject(,"Word.Application")
If objWord Is Nothing Then
	Set objWord = CreateObject("Word.Application")
	Set objDoc = objWord.Documents.Open("C:\Documents and Settings\lspaz\Desktop\Test Report Automation\XXX.docx")
Else
	Set objDoc = objWord.activedocument
End If

	SetBold
	SetRestBold
	SetRestBold
	SetBoldLevel6
	SetBoldLevel5

'objWord.Quit 

Sub SetBold()

    Const Mark = "BHN-"
    
    Dim Rng
    Dim Rstart
    Dim Rend
    Dim Fnd
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, objdoc.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = objdoc.Range(Rstart + Len(Mark) + 1)
        Rend = GetStart(") -", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = objdoc.Range(Rstart, Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub

Sub SetRestBold()

    Const Mark = "Score:"
    
    Dim Rng
    Dim Rstart
    dim Rend
    Dim Fnd
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, objdoc.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = objdoc.Range(Rstart + Len(Mark) + 1)
        Rend = GetStart(" - ", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = objdoc.Range(Rstart, Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub
Sub SetBoldLevel6()

    Const Mark = "LEVEL 6 Events:"
    
    Dim Rng
    Dim Rstart
    dim Rend
    Dim Fnd
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, objdoc.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = objdoc.Range(Rstart + Len(Mark) + 1)
        Rend = GetStart("LEVEL 6 Events:", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = objdoc.Range(Rstart, Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub
Sub SetBoldLevel5()

    Const Mark = "LEVEL 5 Events:"
    
    Dim Rng
    Dim Rstart
    dim Rend
    Dim Fnd
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, objdoc.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = objdoc.Range(Rstart + Len(Mark) + 1)
        Rend = GetStart("LEVEL 5 Events:", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = objdoc.Range(Rstart, Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub

Private Function GetStart(ByVal Txt, _
                          Rng, _
                          Rstart)
    With Rng.Find
        .Execute Txt,,,,,,True
        If .Found Then GetStart = .Parent.Start
    End With
End Function

Open in new window


Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39797916
Please note I added the missing getStart function and some code at the start to get an open document to make testing easier but deleting lines 6,7 & 10-12 will of course re-instate your code for the specific document in a new instance of word

Chris
0
 

Author Comment

by:Maliki Hassani
ID: 39797975
That is brilliant!  I was having such a hard time  Thank you so much..  It works, I will let you know if I have any questions though!
0
 

Author Closing Comment

by:Maliki Hassani
ID: 39798472
Thank you!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

809 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