PDA

View Full Version : Toolbar and add ins as Macro



fadib
05-05-2013, 01:03 PM
Hey guys,
Maybe a little bit off the topic of visual Basic.

I have created a VBA code, that I wish to have it added to the toolbar as an icon.
Where should I put the code, or how to create the file, so that everytime I open a new word document the tool (The macro created) is available.

Thanks for your help.

fadib
05-05-2013, 09:43 PM
Ok, I have done some research and was able to add a toolbar in Normal.dot document template.

The issue I am facing and unable to resolve, is the vba code is failing with Run-time error'4248' This command is not available because no document isopen.
After clicking on Debug With ActiveDocument.Content.Duplicate is highlighted.
The module still trying to reference Normal.dot instead of the opened document.

Dim StartWord As String, EndWord As String
StartWord = "SECTION 7"
EndWord = "SECTION 8"

ThisDocument.Bookmarks("\EndOfDoc").Select
With ActiveDocument.Content.Duplicate
.Find.Execute FindText:=StartWord & "*" & EndWord, MatchWildcards:=True, Forward:=False
.MoveStart wdCharacter, Len(StartWord) - 10
.MoveEnd wdCharacter, -Len(EndWord) - 1
.Select
End With
Selection.TypeBackspace
ActiveDocument.TablesOfContents(1).Update

fumei
05-06-2013, 11:21 PM
Why are you using Duplicate?

fadib
05-07-2013, 01:35 AM
Why are you using Duplicate?

I wasn't sure how to search between two ranges. Searching on the net suggested .Duplicate.

Is there any alternatives? Is this why it is failing?

fumei
05-08-2013, 03:18 PM
But you are not actually searching between ranges. What ranges did you think you were searching between?

Can you explain what you are trying to make happen?

I do not know why you are using EndOfDoc bookmark select.
I do not know why you are using the variables StartWord and EndWord, as you have hard coded them, and your FindText could just as easily be:

FindText:="SECTION 7*SECTION 8" - as that you what you are in fact doing.

I do not know why you are using:
Len(StartWord) - 10

as this will ALWAYS be -1. Len (StartWord) will ALWAYS be equal to 9. "SECTION 7" = 9 characters.

9 - 10 = -1

Please describe what you want to happen.

fumei
05-08-2013, 03:20 PM
Oh, and this has nothing to do with your original subject (the toolbars), perhaps start a new thread. If you have in fact solved the issue of the Subject (toolbars) please mark the thread as Solved.

fadib
05-10-2013, 05:33 AM
I wish I solved the issue.
OK. So I have a document with multiple section 1 through 8
I need to get ride of section 7 and its content. So I locate Section 7 and Section 8 and then delete anything in between.

I started from the end, cause I have two section 7, and I need to delete the second one. If I start from the top, both section 7 will be deleted.
Section 7 - Part 1
Section 7 - Part 2
Section 8
I hope this answers what i am trying to do.

The issue though is not here, it is in the way VBA is recognizing an instance of a document versus the Template.

A cleanup.DOT is used in the STARTUP folder, which as you know will initiate with every new document.
Cleanup.dot will have the code referenced above.
If VBA code is embedded in the instance document, it works fine.
On the other hand, if you run the macro of the .dot file while the instance document s open, it fails.
Why do I need to run the code from the .dot file? Because .dot file and its macro is initiated on every start-up, and will be available for any document you try to use.
I hope this clears some of the vagueness.

fumei
05-10-2013, 07:38 AM
And this has what to do with SUBJECT: Toolbar and add ins as Macro


On the other hand, if you run the macro of the .dot file while the instance document s open, it fails.

Fails how?

So I have a document with multiple section 1 through 8
It does not look that way. It looks like you have TEXT (Section 7 - Part 1), not Word Sections. Deleting real Sections is fairly easy.

If the dot file is in Startup, and the code actions ActiveDocument, then it actions ActiveDocument. So if the active document is what you want to action, then it should do that.

Please post your entire code.

fadib
05-16-2013, 12:02 PM
Sub Local_RepoClean()

'Remove Section 7
Dim StartWord As String, EndWord As String
StartWord = "SECTION 7"
EndWord = "SECTION 8"

ThisDocument.Bookmarks("\EndOfDoc").Select

With ActiveDocument.Content.Duplicate
.Find.Execute FindText:=StartWord & "*" & EndWord, MatchWildcards:=True, Forward:=False
.MoveStart wdCharacter, Len(StartWord) - 10
.MoveEnd wdCharacter, -Len(EndWord) - 1
.Select
End With
Selection.TypeBackspace
ActiveDocument.TablesOfContents(1).Update

'Remove Uneeded table
Dim iResponse As Integer
Dim tTable As Table
Dim i As Integer
i = 0
j = 0
'If any tables exist, loop through each table in collection.
For Each tTable In ActiveDocument.Tables
i = i + 1
If i = 3 Then
tTable.Select
End If
Next

For j = 0 To 14
Selection.TypeBackspace
Next
'Selection.TypeText Text:="N/A" & Chr(11)

MsgBox ("Cleanup complete")


ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

End Sub

fumei
05-16-2013, 01:16 PM
As you are clearly not paying any attention to anything I have posted, I will leave this to someone else to possibly help you.

I would recommend - again - that you post things that actually have something to do with your subject.

Good luck.