VB How To:

Can I take it that you are trying to go to a bookmark that is a number, read it, increment it by one, and then write the value back?

The problem is that a bookmark is defined to be of "text" type, and doesn't have a "value" property. What you need to do is select the bookmark text, and convert that text to an integer.



Code:
Sub Auto_open()

Dim orderNum As Integer
    
Selection.GoTo What:=wdGoToBookmark, Name:="mybm"
ActiveDocument.Bookmarks("mybm").Select
orderNum = CInt(Selection.Text) + 1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InsertAfter orderNum
ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
   Name:="mybm"
   
End Sub

:)
 
Great! Thanks a bunch, ainwood! :)

I really got frustrated with this one. You see, Microsoft had an example on the MSDN pages which used Autotext entries to do the thing, but it ended in an overflow error after a few tries. :mad: I mean, what kind of developers are that!

This solution is much simpler and more elegant. :)
 
For reference, an "overflow" error occurs when you try to put a value into a variable of the wrong type. For example, an "integer" type can hold a maximum value of 32767. If you try and put a number greater than this into an integer data type, you get an "overflow". :)
 
ainwood.

Please check your PM's about my offer. Also, how did you format your code before to include proper vb colors :confused:

Also, I've created a replacement for the Common Dialog box. It includes saving and opening abilities and uses all native controls so it doesn't require any extra runtimes.

Here is the code if you want to browse it. :)
And I'll upload the form shortly. It's basically complete. Unless I or anyone else finds a problem.

Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This Form Created By: Thomas Hawkins, CornEmpire Software
' This can be used freely.
'
' Common Dialog Replacement for Open and Save Functions v1.0
' Also includes a prompt if a file exists and will be overwritten
'
' To use in your program you must call the form and then the
' style you want displayed.
' For an open file box:
' opendialog.Show
' opendialog.opendialogbox
'
' For a Save Box:
' opendialog.Show
' opendialog.savedialogbox
'
' This formats the buttons for the style you need.
'
' You will also have to configure the file types, patterns
' and open/save code
'
' *For Form_Load you will enter the default pattern and sellected
' option in the combo box.  Then add all other options for the
' combo box.
' *For Combo1_Click you will need to list the patterns for each
' item added to the combo box on form load IN ORDER.
' *For openfile and savefile subs...this is where you type
' your open and save code.  It will be used by the form when
' needed.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public strfilename As String, strfilepath As String, strsavefilename As String
Dim pathcheck

Private Sub openfile()
'Code to open file goes here
MsgBox strfilepath
End Sub

Private Sub savefile()
'Code to save file goes here
MsgBox strfilepath
End Sub

Public Sub opendialogbox()
opendialog.location.Caption = "Select Location"
opendialog.filetype.Caption = "Open File of Type:"
opendialog.Caption = "Open File"
opendialog.Command1(0).Caption = "Open"
opendialog.filename.Locked = True
opendialog.filelist.SetFocus
opendialog.file_name.Caption = "File Path"
End Sub

Public Sub savedialogbox()
opendialog.location.Caption = "Select Location"
opendialog.filetype.Caption = "Save File as Type:"
opendialog.Caption = "Save File"
opendialog.Command1(0).Caption = "Save"
opendialog.filename.Locked = False
opendialog.filename.SetFocus
opendialog.file_name.Caption = "Save as File Name:"
End Sub

Private Sub pathchecker()
' Checks the paths to the files to insure no double \\'s
' will be present
pathcheck = Dirlist.List(Dirlist.ListIndex)
pathcheck = UCase(pathcheck) ' Converts drive to upper case
If Command1(0).Caption = "Save" Then
 If pathcheck = "A:\" Or pathcheck = "B:\" Or pathcheck = "C:\" Or pathcheck = "D:\" Or pathcheck = "E:\" Or pathcheck = "F:\" Or pathcheck = "G:\" Or pathcheck = "H:\" Or pathcheck = "I:\" Or pathcheck = "J:\" Or pathcheck = "K:\" Or pathcheck = "L:\" Or pathcheck = "M:\" Or pathcheck = "N:\" Or pathcheck = "O:\" Or pathcheck = "P:\" Or pathcheck = "Q:\" Or pathcheck = "R:\" Or pathcheck = "S:\" Or pathcheck = "T:\" Or pathcheck = "U:\" Or pathcheck = "V:\" Or pathcheck = "W:\" Or pathcheck = "X:\" Or pathcheck = "Y:\" Or pathcheck = "Z:\" Then
  strfilepath = Dirlist.List(Dirlist.ListIndex) & filename.Text
 Else
  strfilepath = Dirlist.List(Dirlist.ListIndex) & "\" & filename.Text
 End If
ElseIf Command1(0).Caption = "Open" Then
 If pathcheck = "A:\" Or pathcheck = "B:\" Or pathcheck = "C:\" Or pathcheck = "D:\" Or pathcheck = "E:\" Or pathcheck = "F:\" Or pathcheck = "G:\" Or pathcheck = "H:\" Or pathcheck = "I:\" Or pathcheck = "J:\" Or pathcheck = "K:\" Or pathcheck = "L:\" Or pathcheck = "M:\" Or pathcheck = "N:\" Or pathcheck = "O:\" Or pathcheck = "P:\" Or pathcheck = "Q:\" Or pathcheck = "R:\" Or pathcheck = "S:\" Or pathcheck = "T:\" Or pathcheck = "U:\" Or pathcheck = "V:\" Or pathcheck = "W:\" Or pathcheck = "X:\" Or pathcheck = "Y:\" Or pathcheck = "Z:\" Then
  strfilepath = Dirlist.List(Dirlist.ListIndex) & filelist.List(filelist.ListIndex)
 Else
  strfilepath = Dirlist.List(Dirlist.ListIndex) & "\" & filelist.List(filelist.ListIndex)
 End If
End If
End Sub

Private Sub Combo1_Click()
'load file patterns based on item selected.  ListIndex = "0"
'is the first item in the combo box.  ListIndex = "1" is the
'second item, etc.
'These Indices are loaded on form load under "Load All File Types"
If Combo1.ListIndex = "0" Then
 filelist.Pattern = "*.html;*.htm"
ElseIf Combo1.ListIndex = "1" Then
 filelist.Pattern = "*.*"
End If
End Sub

Private Sub command1_Click(Index As Integer)
If Index = "0" Then
 If Command1(0).Caption = "Open" Then
  strfilename = filelist.List(filelist.ListIndex)
  pathchecker
  openfile
 ElseIf Command1(0).Caption = "Save" Then
  strsavefilename = filename.Text
  pathchecker
  'Checks to see if file exists, and prompts to overwrite
   Set fso = CreateObject("Scripting.FileSystemObject")
   If fso.FileExists(strfilepath) Then
    If MsgBox("File Already Exists.  Overwrite?", vbYesNo Or vbCritical, "File Exists") = vbYes Then
     savefile
    End If
   Else
    savefile
   End If
  End If
ElseIf Index = "1" Then
 Unload opendialog
End If
End Sub

Private Sub Dirlist_Change()
filelist.Path = Dirlist.Path
End Sub

Private Sub Drivelist_Change()
Dirlist.Path = Drivelist.Drive
End Sub

Private Sub filelist_Click()
'On Open writes path to filename box.  On save, writes file name
'to filename box.
If file_name.Caption = "File Path" Then
 pathchecker
 filename.Text = strfilepath
ElseIf file_name.Caption = "Save as File Name:" Then
 filename.Text = filelist.List(filelist.ListIndex)
End If
End Sub

Private Sub Form_Load()
'load default file pattern/Type
filelist.Pattern = "*.html;*.htm"
Combo1.Text = "Webpages (*.html;*.htm)"
'Load All File Types
Combo1.AddItem "Webpages (*.html;*.htm)"
Combo1.AddItem "All Files (*.*)"
End Sub
 
Corn,

Yep got it :D

You may want to edit you code above - its making this page rather wide :lol:.

re the colour formatting, I wrote some code that just parses the text and converts it to put the little {color=XXX} {/color} tags into the code. It doesn't work properly, so I then have to edit it a bit. I'll post it when I get back to work. Have you ever used PretyCodePrint?? It basically lets you print your code on a colour printer, plus links the For/next & If/then loops. I think you can download it at www.vbcity.com???

P.S. Like your common dialog :)
 
Haven't had much time lately to add to this thread.

Firstly, some news! I have joined Cornmaster at "CornEmpire software". Haven't yet been able to add anything to his site yet, but come visit it (the link is in my sig :) )



Anyway, here's some code to retrieve the temporary directory:

Code:
[color=blue]Private Declare Function[/color] GetTempPathA [color=blue]Lib[/color] "kernel32" _
   ([color=blue]ByVal[/color] nBufferLength [color=blue]As Long[/color], [color=blue]ByVal[/color] lpBuffer [color=blue]As String[/color]) [color=blue]As Long[/color]
 
[color=blue]Public Function[/color] GetTempPath() [color=blue]As String[/color]
[color=blue]Dim[/color] lngSize [color=blue]As Long[/color]
[color=blue]Dim[/color] strTemp [color=blue]As String[/color]
[color=blue]Dim[/color] lngLength [color=blue]As Long[/color]
 
strTemp = Space$(256)
lngSize = Len(strTemp)
lngLength = GetTempPathA(lngSize, strTemp)
GetTempPath = Left(strTemp, lngLength)
    
[color=blue]End Function[/color]




P.S. Corn - can you edit your sub above to decrease the width of this page :lol:
 
We have a lan at home, and I thought that it would be a good idea to make a program that connects the two computers. It maps drives, I can shut down the other computer and other useful/less useful stuff. I also tried to see if I got send whatever is in the clipboard to the other computer's clipboard, and it works perfect as long as the clipboard contains text. I solved the pictureproblem by saving the picture to a file and copy it to the other computer where I load it into the clipboard, which only works if the drives are mapped. Do you know any good way to send a picture (IPictureDisp) or any other class through a winsock connection.

I downloaded an example from a VB site where I could send files through winsock connections, splitting it up in chunks before sending, but I would like to transfer the picture without saving it as a file at all.

Grateful for any help:)
 
This is the way I do it now, I haven't tested it very much though:

Computer A:
Code:
SavePicture Clipboard.GetData(), "copypic.bmp"
FileCopy "copypic.bmp", "j:\clas\copypic.bmp"
ws.SendData "ok"
Computer B:
Code:
Dim pic As IPictureDisp
ws.GetData s
if s = "ok" then
Set pic = LoadPicture("c:\clas\copypic.bmp")
Clipboard.SetData pic
end if

As I said I have a codesample that splits the file and send it in bytes, but I still have to save it as a file. Apparently I can't send data of the type "object" with winsock.:)
 
If you're in to APIs you probably like to use BitBlt, but there is a VB version of this function called PaintPicture. It's a method in Picturebox, form and Printer objects and paints a part of or the whole picture in another object.
object.PaintPicture picture, x1, y1, width1, height1, x2, y2, width2, height2, opcode

Object: The object to paint the new picture in
Picture: Source picture, from a form, picturebox
x1, y1: Indicates the coordinates where to start painting the new picture
width1, height1: New picture's size. Should be omitted unless you want to resize the picture.
x2, y2: Coordinates in source picture where to start clipping the picture
width2, height2: Size of the clip from source picture.
opcode: Bitwise operations performed on the picture before painting it. E.g. vbSrcCopy, vbMergeCopy, SrcAnd

The scalemode property determines what units to measure in. E.g. points, pixels, twips, mm

Code:
destinationpic.PaintPicture sourcepic, 35, 40,10 ,15 , 0, 5, 20, 30, vbSrcAnd

This command will copy a picture from sourcepic to destinationpic. A square starting at x45 and y40 in sourcepic with the size w10 and h15 will be copied to fill a square starting at x0 and y5 in destinationpic with the size w20 and h30. The new picture will be twice as large as the old picture (20/10 and 30/15).
Opcodes can only be used with bitmaps. The opcode in this example will paint the new picture by taking a point from the source and add it to the destination with the operator AND.

11110000
10101010AND
10100101

The default opcode is SrcCopy, which paints the new picture exactly the same as the source picture.

I hope this is clear enough:)
 
1) How to set up a progress bar in excel


Set up a userform "ProgressBar" with too labels on to of each other, lbFixed and lbindicate.

Add the following code to the form

Sub pcProgress(pcTitle As String, iPercent As Integer)
ProgressBar.Caption = pcTitle
lbIndicate.Width = iPercent / 100 * lbFixed.Width
DoEvents
End Sub

From main module

'Start progress Bar


for myValue = 1 to 10,000

ProgressBar.Show vbModeless
PartComplete = Int(myValue * 100 / 10,000)
TempTitle = "Current Progess"
ProgressBar.pcProgress TempTitle, PartComplete

' other code goes here

Next myValue

unload ProgressBar

' As the form is loaded vbModeless then the rest of the code will continue to execute. Label lbIndicate grows in width as myValue increases until it covers lbFixed. The form is then unloaded.

2) Use of Status bar

If you are using application.screenupdating = false to speed up your code then you might try using the status bar to keep the user informed.

application.statusbar = "Your Message"

don't forget to end sub with
application.statusbar = false
to return control to the program


ferenginar
 
How to automatically select the text in a textbox when a user clicks in it:

Put the following code in the "GotFocus" event of a textbox (called "txtMyText"

Code:
Sub txtMyText_GotFocus()
    txtMyText.SelStart = 0
    txtMyText.SelLength = Len(txtMyText.text)
End Sub
 
Some windows API calls require that you pass the handle of calling object as a parameter. However, in VBA the handle is not a standard property of an object. So what do you do?

If you want the handle to something, you can simply pass its caption to the FindWindow API. Very easy!


Code:
[color=blue]Public Declare Function[/color] FindWindow [color=blue]Lib[/color] "user32" [color=blue]Alias[/color] "FindWindowA" _
    ([color=blue]ByVal[/color] lpClassName [color=blue]As String[/color], [color=blue]ByVal[/color] lpWindowName [color=blue]As String[/color]) [color=blue]As Long[/color]


[color=blue]Private Sub[/color] UserForm_Initialize()
[color=blue]Dim[/color]lngFormHwnd [color=blue]As Long[/color]
lngFormHwnd = FindWindow(vbNullString, Me.Caption)
[color=blue]End Sub[/color]

You may even find a use for this approach in VB (getting the handle to a command button maybe?:)
 
I have a few questions.

1) Is there any way to load compressed images into a VB Resource file (JPEG, GIF... files)
2) How do you get a form to be a specific shape?

I'd appreciate any help I can get!
 
Re the compressed formats, it can be done, but I believe that you need a custom control.... Basically, these formats are licensed, so MS doesn't ship controls that support them - you need to pay. Not completely sure, so perhaps you can have a look on the web. :)

Getting a form to be a specific shape is do-able, but requires the use of the windows API. I'm being a bit lazy here in not giving sample code, but you can see some sample code (with comments in french :mischief: here :)
 
Back
Top Bottom