Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Same Code Doesn't Work in Different Word doc

Status
Not open for further replies.

Boots6

Technical User
Aug 12, 2011
91
US
I use this code in one document and it works fine:
Sub Subcontracter()
'
' Subcontracter Macro
'Dim strSubName As String
Dim i As Integer

strSubName = InputBox("Subcontractor")

With Selection
For i = 1 To 2
.GoTo What:=wdGoToBookmark, Name:="SubContr" & i
.TypeText Text:=strSubName
Next i

End With
End Sub

This is the macro I put in to the other file:

Sub Inserts()
'
' Inserts Macro

'Dim strProName As String
Dim i As Integer
strProjName = InputBox("Project Name")

With Selection
For i = 1 To 5
.GoTo What:=wdGoToBookmark, Name:="ProjName" & i
.TypeText Text:=strProjName
Next i
End With

End Sub

Every time it says Bookmark Does Not Exist, but it does. I'm not totally stupid, the name is exactly the same "ProjName" - I've checked a billion times. Any ideas?
 

" the name [of a bookmark, I guess] is exactly the same "ProjName" - I've checked a billion times"

Well, the name should NOT be ProjName, it should be:
ProjName1
ProjName2
ProjName3
ProjName4
ProjName5

Have fun.

---- Andy
 
I can try that, but then why does this macro where I named bookmarks the same way ("SubContr", SubContr1", SubContr2", etc)work?:

Sub LongSub()
'
' LongSub Macro
'' SubcontractorLong Macro

'Dim strDate As String

strDate = InputBox("Today's Date")

With Selection
For i = 1 To 5
.GoTo What:=wdGoToBookmark, Name:="Date" & i
Selection.Font.Color = wdColorBlue
Selection.Font.Bold = wdToggle
.TypeText Text:=strDate
Next i
End With

'Dim strSubContr As String

strSubContr = InputBox("Subcontractor")

With Selection
For i = 1 To 10
.GoTo What:=wdGoToBookmark, Name:="SubContr" & i
Selection.Font.Color = wdColorBlue
Selection.Font.Bold = wdToggle
.TypeText Text:=strSubContr
Next i
End With


'Dim strSubStrAdd As String

strSubStrAdd = InputBox("Sub Street Address")

With Selection
For i = 1 To 6
.GoTo What:=wdGoToBookmark, Name:="SubStrAdd" & i
Selection.Font.Color = wdColorBlue
Selection.Font.Bold = wdToggle
.TypeText Text:=strSubStrAdd
Next i
End With

End Sub
 

Your booksmark "SubContr" should NOT get any value because you do:
[tt]
With Selection
For i = 1 To 10
.GoTo What:=wdGoToBookmark, Name:="SubContr" & i
Selection.Font.Color = wdColorBlue
Selection.Font.Bold = wdToggle
.TypeText Text:=strSubContr
Next i
End With
[/tt]
which will only populate SubContr1 to SubContr10 bookamrks, and not SubContr bookmark.

I know it is picky on my end, but that's what computers do as well.... :)

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top