Solved

VBA to VB Script - Code modify

Posted on 2014-01-21
4
562 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now